Results 1 to 2 of 2

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

  1. #1
    Join Date
    Jun 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Can't exit out of loop to proceed to next lines of code - Button Pressed Event

    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.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QColor>
    4. #include <iostream>
    5. #include <QByteArray>
    6. #include <QDebug>
    7. #include <vector>
    8. #include <stdio.h>
    9. #include <algorithm>
    10. #include <string>
    11.  
    12. MainWindow::MainWindow(QWidget *parent) :
    13. QMainWindow(parent),
    14. ui(new Ui::MainWindow)
    15. {
    16. ui->setupUi(this);
    17.  
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25.  
    26.  
    27. void MainWindow::on_cleanPlate_clicked()
    28. {
    29.  
    30. // load multiple images - must be same dimensions
    31. QStringList filename = QFileDialog::getOpenFileNames(this, tr("Choose Image"), "", tr("Images (*.png *.jpg *.jpeg *.bmp)"));
    32.  
    33. uint numberOfImages = static_cast<uint>(filename.count());
    34.  
    35. QString sizeQuery = filename.at(0);
    36. QImage getImageSize(sizeQuery);
    37. getImageSize = getImageSize.convertToFormat(QImage::Format_RGBA8888);
    38. uint numberOfBytes = static_cast<uint>(getImageSize.sizeInBytes());
    39.  
    40. std::vector<std::vector<std::vector<uint>>> rgb(numberOfImages, std::vector<std::vector<uint>>(numberOfBytes / 4, std::vector<uint>(4,0)));
    41. std::vector<std::vector<std::vector<uint>>> rgbTranspose(numberOfBytes/4, std::vector<std::vector<uint>>(numberOfImages, std::vector<uint>(4,0)));
    42.  
    43. // start of nested loop
    44. for (uint i{0};i < numberOfImages;++i) {
    45.  
    46. QString pictures = filename.at(i);
    47. QImage image(pictures);
    48. image.load(pictures);
    49. image = image.convertToFormat(QImage::Format_RGBA8888);
    50.  
    51. auto const pictureRGBdata = image.bits();
    52. for (uint j{0};j < numberOfBytes/4; ++j) {
    53.  
    54. std::cout << "Image " << i << ": Pixel: " << j;
    55.  
    56. for (uint k{0};k < 4;++k) {
    57.  
    58. rgb[i][j][k] = pictureRGBdata[4*j +k];
    59. std::cout << " " << rgb[i][j][k];
    60.  
    61. }
    62. std::cout << std::endl;
    63. }
    64. std::cout << std::endl;
    65. }
    66. //end of nested loop - but loop gets stuck here - does not exit loop to next line of code
    67. std::cout << "This won't print ";
    68.  
    69.  
    70. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can't exit out of loop to proceed to next lines of code - Button Pressed Event

    Ok, this is now solved simply by replacing std::cout with qDebug() instead. Apparently std::cout can cause a buffer problem or something.

Similar Threads

  1. Replies: 2
    Last Post: 13th November 2011, 03:30
  2. Replies: 6
    Last Post: 4th October 2010, 04:19
  3. Exit unending loop with Button
    By Roelof in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2009, 14:20
  4. Raise key pressed event when button is clicked?
    By newstead in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2009, 14:12
  5. Delay Loop until a QPushButton pressed
    By ljshap in forum Newbie
    Replies: 6
    Last Post: 17th January 2009, 17:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.