Just a quick question. I'm developing a QT Console application. Now, I'd like to run it until the user presses any key (or, if it is easier, until the enter key is pressed).
The application has a running event queue (or better two, one for the "main", the other for the data parser).
Here is a mock up of the relevant part of the code:
main.cpp
#include <QCoreApplication>
#include <QTimer>
#include "mainprogram.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QCoreApplication::setApplicationVersion("1.0");
MainProgram mainProg(&a);
QTimer::singleShot(0, &mainProg, SLOT(executeMain()));
return a.exec();
}
mainprogram.h
#ifndef MAINPROGRAM_H
#define MAINPROGRAM_H
#include <QObject>
#include <QThread>
#include "datasourceparser.h"
class MainProgram : public QObject
{
Q_OBJECT
public:
explicit MainProgram(QObject *parent = 0);
~MainProgram();
public slots:
void executeMain();
void terminateProgram(int resultCode);
private:
DataSourceParser *dataParser;
QThread dataParserThread;
};
#endif // MAINPROGRAM_H
mainprogram.cpp
#include "mainprogram.h"
#include <QCoreApplication>
MainProgram::MainProgram(QObject *parent) : QObject(parent)
{
}
MainProgram::~MainProgram() {
dataParserThread.quit();
dataParserThread.wait();
}
void MainProgram::executeMain()
{
dataParser = new DataSourceParser();
dataParser->moveToThread(&dataParserThread);
QObject::connect(&dataParserThread, SIGNAL(started()), dataParser, SLOT(execute()));
QObject::connect(&dataParserThread, SIGNAL(finished()), dataParser, SLOT(deleteLater()));
dataParserThread.start();
}
void MainProgram::terminateProgram(int resultCode)
{
QCoreApplication::exit(resultCode);
}
Now, the DataSourceParser
does all the elaboration. If something fails, it clls the MainProgram::terminateProgram
slot to properly exit, and everything works.
Now, I need something which calls that slot when the user presses a key (or just enter, if easier).
This solution should be cross platform (or, at least, Windows and Linux x86/x64)
And, most important, it should NOT block the main thread events queue (so a readline inside the executeMain() function is not feasible).
Thank you
EDIT:
As Karsten Koop, I tried to add the QSocketNotifier implementation found here: I copied the header and source file content (just replaced the #pragma once
with the usual #ifndef CONSOLEREADER_H
etc). Then, before the call to dataParser->moveToThread(&dataParserThread);
, I added
ConsoleReader *cr = new ConsoleReader(this);
QSignalMapper *signalMapper = new QSignalMapper(this);
QObject::connect(cr, SIGNAL(textReceived(QString)), signalMapper, SLOT(map()));
signalMapper->setMapping(cr, 1);
this->connect(signalMapper, SIGNAL(mapped(int)), SLOT(terminateProgram(int)));
However the text slot is never called..