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.
protected:
bool processEvents
(QEventLoop::ProcessEventsFlags flags
);
bool hasPendingEvents();
void interrupt();
void registerTimer
(int timerId,
int interval,
QObject* obj
);
QList<TimerInfo> registerTimers
(QObject* obj
) const;
bool unregisterTimer(int timerId);
bool unregisterTimers
(QObject* obj
);
void wakeUp();
void flush();
int sockFd = notifier->socket();
int type = notifier->type();
connect(notifier, SIGNAL(activated(int)), this, (SLOT(handleRead(int)));
connect(notifier, SIGNAL(activated(int)), this, SLOT(handleWrite(int)));
// handle exception
}
private Q_SLOTS:
void handleRead(int sock);
void handleWrite(int sock);
};
class MyEventDispatcher : public QAbstractEventDispatcher {
protected:
bool processEvents(QEventLoop::ProcessEventsFlags flags);
bool hasPendingEvents();
void interrupt();
void registerTimer(int timerId, int interval, QObject* obj);
QList<TimerInfo> registerTimers(QObject* obj) const;
bool unregisterTimer(int timerId);
bool unregisterTimers(QObject* obj);
void wakeUp();
void flush();
void registerSocketNotifier(QSocketNotifier* notifier) {
int sockFd = notifier->socket();
int type = notifier->type();
if (type == QSocketNotifier::Read)
connect(notifier, SIGNAL(activated(int)), this, (SLOT(handleRead(int)));
else if (type == QSocketNotifier::Write)
connect(notifier, SIGNAL(activated(int)), this, SLOT(handleWrite(int)));
else if (type == QSocketNotifier::Exception)
// handle exception
}
void unregisterSocketNotifier(QSocketNotifier*);
private Q_SLOTS:
void handleRead(int sock);
void handleWrite(int sock);
};
To copy to clipboard, switch view to plain text mode
My main function would look something like this:
int main(int argc, char *argv[]) {
MyEventDispatcher* dispatcher("mainwindow");
MainWindow *mainWindow = new MainWindow();
mainWindow->show();
//MyApp::getInstance().run(argc, argv);
//return 0;
return app.exec();
}
int main(int argc, char *argv[]) {
MyEventDispatcher* dispatcher("mainwindow");
QApplication app(argc, argv);
MainWindow *mainWindow = new MainWindow();
mainWindow->show();
//MyApp::getInstance().run(argc, argv);
//return 0;
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Sudo code of my own main event loop:
class MyApp {
public:
static MyApp& getInstance();
...
void run(int argc, char* argv[]) {
while (mShouldRun)
runOnce();
}
protected:
void runOnce() {
int rc = select(size, &readFds, &writeFds, 0, &timeout);
if (rc > 0) {
for (int i = 0; i < mFdSet.size(); i++) {
int fd = mFdSet.at(i);
if (FD_ISSET(fd, &readFds)) read();
}
for (int i = 0; i < mFdSet.size(); i++) {
int fd = mFdSet.at(i);
if (FD_ISSET(fd, &writeFds)) write();
}
}
}
};
class MyApp {
public:
static MyApp& getInstance();
...
void run(int argc, char* argv[]) {
while (mShouldRun)
runOnce();
}
protected:
void runOnce() {
int rc = select(size, &readFds, &writeFds, 0, &timeout);
if (rc > 0) {
for (int i = 0; i < mFdSet.size(); i++) {
int fd = mFdSet.at(i);
if (FD_ISSET(fd, &readFds)) read();
}
for (int i = 0; i < mFdSet.size(); i++) {
int fd = mFdSet.at(i);
if (FD_ISSET(fd, &writeFds)) write();
}
}
}
};
To copy to clipboard, switch view to plain text mode
My Qt widget app
// GUI stuff
}
class MainWindow : public QMainWindow {
// GUI stuff
}
To copy to clipboard, switch view to plain text mode
Bookmarks