Combing an External Event Loop with Qt's


I'm building a Qt client for the open-source client/server 4X strategy game Thousand Parsec. This a Google Summer of Code project. I'm however stuck at a dead end. Basically, the client interfaces with the server via a C++ protocol layer that facilitates client/server communication. The protocol's documentation is available here.

Now my problem is that the protocol requires you to create a subclass of the virtual EventLoop class (link) in your client. There is an example SimpleEventLoop used for console clients on the same link. I'm having difficulty figuring out how I can design my own event loop subclass that handles the protocol's events while hooking into the Qt application at the same time. My research has lead me to believe QAbstractEventDispatcher is the Qt class I want to use but the documentation seems pretty slim and I'm not exactly sure how I would go about doing this.

Does anyone else have experience linking external event loops with a Qt application? I've also found this example on the Qt page but it wasn't very helpful - or at least I didn't really understand it.


9/2/2017 8:15:19 PM

I haven't done too much Qt development recently, but if I remember correctly, you can call QApplication::processEvents() within your own event loop (instead of starting the Qt main loop through QApplication::exec())

Edit: I have used the opportunity of a slow Sunday morning to test-drive / learn something about PyQt (Python bindings for Qt) and cobbled together a proof-of-concept code below. Replacing the call to QApplication::exec() with a custom event loop based on QApplication::processEvents() seems to work.

I have also quickly looked at simpleeventloop.cpp and tpclient-cpptext main.cpp. From the looks of it, it shoud be fine to just add QApplication::processEvents() somewhere in the main loop of SimpleEventLoop::runEventLoop(). To add it to the main loop, I would probably replace the tv interval for the select() function in lines 106 through 117 with

tv.tv_sec = 0;
tv.tv_usec = 10000;   // run processEvents() every 0.01 seconds

and change the signature in line 89 to void SimpleEventLoop::runEventLoop(QApplication *app). It should than be fine to add your usual Qt stuff to your implementation of the client (your replacement of tpclient-cpptext main.cpp)

Looks like a hack, though. I would probably start with something like this to get started. I think that your idea of wrapping TPSocket and the timer within Qt's respective concepts in order to forward them with the QAbstractEventDispatcher to the QEventLoop is the better long-term solution. It should then be sufficient that your runEventLoop() simply calls QApplication::exec(). But I have never used QAbstractEventDispatcher before, so take my comments for what they are.

import sys
import time

from PyQt4 import QtGui
from PyQt4 import QtCore

# Global variable used as a quick and dirty way to notify my
# main event loop that the MainWindow has been exited

class SampleMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        global APP_RUNNING
        APP_RUNNING = True

        # main window
        self.setGeometry(300, 300, 250, 150)

        # exit action (assumes that the exit icon from
        # is saved as Exit.png in the same folder as this file)
        exitAction = QtGui.QAction(QtGui.QIcon('Exit.png')
        exitAction.setStatusTip('Exit application')

        # main menu
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')

        # toolbar
        self.toolbar = self.addToolBar('Exit')

        # text editor
        textEdit = QtGui.QTextEdit()

        #tool tip
        textEdit.setToolTip('Enter some text')
        QtGui.QToolTip.setFont(QtGui.QFont('English', 12))

    def closeEvent(self, event):
        reply = QtGui.QMessageBox.question(self
                                           ,"Are you sure?"

        if reply == QtGui.QMessageBox.Yes:
            global APP_RUNNING
            APP_RUNNING = False

# main program
app = QtGui.QApplication(sys.argv)
testWindow = SampleMainWindow()
# run custom event loop instead of app.exec_()
    # sleep to prevent that my "great" event loop eats 100% cpu
8/12/2016 2:30:30 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow