0

My main.cpp look like this:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

An in my mainwindow.cpp I want to show a different image at each loop in "while", so it would look like this:

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    image = load_an_image
    int i=0;
    while (i<15)
    {
        show image in the MainWindow
        waitkey (wait until I press a key or wait some time)
        do something to this image for the next loop
        i++
    }
}

However the Mainwindow does not show up until the "while" is finished and I cannot find how to show the MainWindow at each loop.

Can anyone give me any advice ?

user3611077
  • 3
  • 1
  • 2
  • 1
    You can handle keyReleaseEvent on your main window, and once you press/release a key, you can change the image and so on. – vahancho May 07 '14 at 07:45
  • Create a slot which morphs you image and displays it. Connect this slot with keypress signal or QTimer::timeout() signal. – Sebastian Lange May 07 '14 at 07:48

2 Answers2

2

GUI will not update itself until gui thread is free of other tasks. However, you can force it using

qApp->processEvents();

Following is example of very bad coding style, but that might be what you want.

#include "mainwindow.h"
#include <QApplication>
#include <thread>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    qint8 k = 15;

    using namespace Qt;

    QPalette pal;
    QColor col = red;

    while (k--)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(250));
        pal.setBrush(w.backgroundRole(), QBrush(col));
        w.setPalette(pal);
        col = col == red ? blue : red;

        qApp->processEvents();
    }
    return a.exec();
}

To run this, you will have to add QMAKE_CXXFLAGS += -std=c++11 to your '.pro' file. And if you want to understand things better, i recommend to read about qt events.

Kirill
  • 65
  • 7
0

You could delay handling the image by using a Qtimer. Something like this: -

MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTimer::singleShot(5, this, SLOT(timeout());
}

// create as a slot in the MainWindow derived class
void MainWindow::timeout()
{
    image = load_an_image();
    int i=0;
    while (i<15)
    {
        // show image in the MainWindow
        // waitkey (wait until I press a key or wait some time)
        // do something to this image for the next loop
        i++
    } 
}

However, it would be better handled by loading the first image and then reacting to key events, rather than waiting directly in the main thread...

void MainWindow::keyReleaseEvent(QKeyEvent* keyEvent)
{
    if(keyEvent->key() == Qt::Key_Space) // use the space bar, for example
    {
        if(m_imageFrame < 15)
        {
             // update the image
        }
    }
    else
    {
        QMainWindow::keyReleaseEvent(keyEvent);
    }        
}
Ali Diouri
  • 86
  • 1
  • 9
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • Thank's a lot. I will try this, it is the first time I use QTcreator so I didn't know what to use in the first place. – user3611077 May 07 '14 at 08:50
  • I cannot make it work... I tried the first solution but it is not working. I think it will still wait the end of the "while" to show the MainWindow. I would like the mainwindow.cpp to interact with the main.cpp Because right now all the code in mainwindow.cpp is computed and then w.show(); shows the interface... But after all the loops are done. – user3611077 May 07 '14 at 12:35
  • Ignore the 1st example, and handle the keyReleaseEvent. Gui applications, such as those with Qt are event based and you should be thinking of working with events this way. – TheDarkKnight May 07 '14 at 12:42