Results 1 to 4 of 4

Thread: QAbstractEventDispatcher for external event loop integration

  1. #1
    Join Date
    Aug 2009
    Posts
    13
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QAbstractEventDispatcher for external event loop integration

    I'm writing an application, which has already it's own main event loop that calls other libraries. Now I want to use Qt as a widget library, and am trying to setup the Qt library for being capable to be called from my own main loop.

    After doing some research, I think the right way to achieve this is to use QAbstractEventDispatcher ("allows the integration of an external event loop with the Qt event loop"). However I'm still not sure how to tie this with my own main loop, and also, I don't know exactly what's needed for the pure virtual functions implementation.

    Hope someone could shed some lights for the newbie here. Thanks in advance.

    Qt Code:
    1. class MyEventDispatcher : public QAbstractEventDispatcher {
    2. protected:
    3. bool processEvents(QEventLoop::ProcessEventsFlags flags);
    4. bool hasPendingEvents();
    5. void interrupt();
    6. void registerTimer(int timerId, int interval, QObject* obj);
    7. QList<TimerInfo> registerTimers(QObject* obj) const;
    8. bool unregisterTimer(int timerId);
    9. bool unregisterTimers(QObject* obj);
    10. void wakeUp();
    11. void flush();
    12. void registerSocketNotifier(QSocketNotifier* notifier) {
    13. int sockFd = notifier->socket();
    14. int type = notifier->type();
    15.  
    16. if (type == QSocketNotifier::Read)
    17. connect(notifier, SIGNAL(activated(int)), this, (SLOT(handleRead(int)));
    18. else if (type == QSocketNotifier::Write)
    19. connect(notifier, SIGNAL(activated(int)), this, SLOT(handleWrite(int)));
    20. else if (type == QSocketNotifier::Exception)
    21. // handle exception
    22. }
    23. void unregisterSocketNotifier(QSocketNotifier*);
    24.  
    25. private Q_SLOTS:
    26. void handleRead(int sock);
    27. void handleWrite(int sock);
    28. };
    To copy to clipboard, switch view to plain text mode 

    My main function would look something like this:
    Qt Code:
    1. int main(int argc, char *argv[]) {
    2. MyEventDispatcher* dispatcher("mainwindow");
    3. QApplication app(argc, argv);
    4. MainWindow *mainWindow = new MainWindow();
    5. mainWindow->show();
    6. //MyApp::getInstance().run(argc, argv);
    7. //return 0;
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Sudo code of my own main event loop:
    Qt Code:
    1. class MyApp {
    2. public:
    3. static MyApp& getInstance();
    4. ...
    5. void run(int argc, char* argv[]) {
    6. while (mShouldRun)
    7. runOnce();
    8. }
    9. protected:
    10. void runOnce() {
    11. int rc = select(size, &readFds, &writeFds, 0, &timeout);
    12. if (rc > 0) {
    13. for (int i = 0; i < mFdSet.size(); i++) {
    14. int fd = mFdSet.at(i);
    15. if (FD_ISSET(fd, &readFds)) read();
    16. }
    17. for (int i = 0; i < mFdSet.size(); i++) {
    18. int fd = mFdSet.at(i);
    19. if (FD_ISSET(fd, &writeFds)) write();
    20. }
    21. }
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 

    My Qt widget app
    Qt Code:
    1. class MainWindow : public QMainWindow {
    2. // GUI stuff
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by vkincaid; 28th August 2009 at 23:59.

  2. #2
    Join Date
    Aug 2009
    Posts
    13
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QAbstractEventDispatcher for external event loop integration

    Sorry for pulling this topic up again, but I seem to be in a dead end for the last couple of days Any pointers about this will greatly be appreciated!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractEventDispatcher for external event loop integration

    If all your loop does is to call select() on a bunch of descriptors and do something based on the result then maybe it might be easier to integrate your loop with Qt's loop and not the other way round. It would be trivial by using QSocketNotifier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following user says thank you to wysota for this useful post:

    vkincaid (31st August 2009)

  5. #4
    Join Date
    Aug 2009
    Posts
    13
    Thanks
    7
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QAbstractEventDispatcher for external event loop integration

    Thanks a lot wysota! I receive socket notification via activated signals now and still use app.exec(). This is a lot easier to understand than using QAbstractEventDispatcher.

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.