PDA

View Full Version : Can't exit out of loop to proceed to next lines of code - Button Pressed Event



Rod
5th June 2019, 09:36
Hi guys,

I am new to Qt and c++. I have written some code which loads multiple images (of same dimensions). The red green blue and alpha data gets copied into a 3 dimensional vector. That all works.

The problem is that it stays stuck in the nested for loop and does not execute the code after the loop until I stop or close. Then it prints to the console as a test. I want to be able to do processing on the data in the vector after the loop executes.

I am very stuck on this.

This is an example of the output using very small test images per image, per pixel, red green blue alpha values.

Image 0: Pixel: 0 181 230 29 255
Image 0: Pixel: 1 255 255 255 255

Image 1: Pixel: 0 233 5 7 255
Image 1: Pixel: 1 0 0 0 255

Here is my code.


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QColor>
#include <iostream>
#include <QByteArray>
#include <QDebug>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

}

MainWindow::~MainWindow()
{
delete ui;
}



void MainWindow::on_cleanPlate_clicked()
{

// load multiple images - must be same dimensions
QStringList filename = QFileDialog::getOpenFileNames(this, tr("Choose Image"), "", tr("Images (*.png *.jpg *.jpeg *.bmp)"));

uint numberOfImages = static_cast<uint>(filename.count());

QString sizeQuery = filename.at(0);
QImage getImageSize(sizeQuery);
getImageSize = getImageSize.convertToFormat(QImage::Format_RGBA88 88);
uint numberOfBytes = static_cast<uint>(getImageSize.sizeInBytes());

std::vector<std::vector<std::vector<uint>>> rgb(numberOfImages, std::vector<std::vector<uint>>(numberOfBytes / 4, std::vector<uint>(4,0)));
std::vector<std::vector<std::vector<uint>>> rgbTranspose(numberOfBytes/4, std::vector<std::vector<uint>>(numberOfImages, std::vector<uint>(4,0)));

// start of nested loop
for (uint i{0};i < numberOfImages;++i) {

QString pictures = filename.at(i);
QImage image(pictures);
image.load(pictures);
image = image.convertToFormat(QImage::Format_RGBA8888);

auto const pictureRGBdata = image.bits();
for (uint j{0};j < numberOfBytes/4; ++j) {

std::cout << "Image " << i << ": Pixel: " << j;

for (uint k{0};k < 4;++k) {

rgb[i][j][k] = pictureRGBdata[4*j +k];
std::cout << " " << rgb[i][j][k];

}
std::cout << std::endl;
}
std::cout << std::endl;
}
//end of nested loop - but loop gets stuck here - does not exit loop to next line of code
std::cout << "This won't print ";


}

Rod
5th June 2019, 20:06
Ok, this is now solved simply by replacing std::cout with qDebug() instead. Apparently std::cout can cause a buffer problem or something.