Results 1 to 4 of 4

Thread: QAbstractEventDispatcher for external event loop integration

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 22:59.

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
  •  
Qt is a trademark of The Qt Company.