tags:

views:

70

answers:

2

I'm starting with QT4, I'm wondering where to put my application code.
Here?

void MainWindow::changeEvent(QEvent *e) {...}

Or here? (where exactly?)

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

If I want my app not only react to user events, but to execute regularly on a loop, where do I put the loop?

+1  A: 

Qt has its own main loop, you can connect to it using the QTimer class.

If you want to provide your own event loop, you can use QApplication::processEvents() to signal Qt to process it's events (and keep a responsible UI).

Also, QAbstractEventDispatcher might be useful to you.

This question might also be useful.

Matias Valdenegro
+2  A: 

Unless you loop within a non-gui thread, you will block the GUI by looping (in the implicit main gui thread). Here's a couple of different approaches:

  1. Use threads. Qt's signals and slots are thread safe. Thus, within a thread you can call your emulator (which may block) and which will then return data to the calling thread. You can then emit a signal to the GUI thread which will respond to the signal and update the GUI accordingly.
  2. Use a timer. You can use QTimer (or a singleShot timer) set to a delay of zero milliseconds. This has the affect of calling your slot as often as it possibly can while not blocking the loop. If the slot returns quickly this will not appear to block the GUI and simplifies programming a little bit.

There are other different approaches, such as using processEvents() but I'd personally recommend the threads approach.

Kaleb Pederson