开发者

Where to put app's code on a QT app?

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 event开发者_JAVA技巧s, but to execute regularly on a loop, where do I put the loop?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜