0

i'm using C++ and Qt4.5.2 (requirement of the target system) in my project. There is a need of a worker thread supplied with additional arguments/parameter, so i did the following (in short with deleted error handling/output):

bool _bRetVal = false;
COpgThread* _pParseThread = new COpgThread(); // COpgThread is subclassed from QThread to access the protected msleep-function
COpgWorker* _pParseWorker = new COpgWorker(); // COpgWorker is subclassed from QObject to provide: slots (process) and signals (finished, progress, error) for a COpgThread-instance
QSignalMapper* _pParseMapper = new QSignalMapper();
COpgParam _pParseParam = new COpgParam(m_sParseFile, m_sRegExpFile); // COpgParam is subclassed from QObject to handle user data for mapping

_pParseWorker->moveToThread(_pParseThread);
//#1 hookup error-signal from worker to errorParsing-function in this object
_bRetVal = QObject::connect(_pParseWorker, SIGNAL(error(int const)), this, SLOT(errorParsing(int const)));

//#2..3 hookup thread's started-signal to process-slot in the worker (via signal mapper for parameters/arguments)
_bRetVal = QObject::connect(_pParseThread, SIGNAL(started()), _pParseMapper, SLOT(map()));
_pParseMapper->setMapping(_pParseThread, _pParseParam);
_bRetVal = QObject::connect(_pParseMapper, SIGNAL(mapped(QObject*)), _pParseWorker, SLOT(process(QObject*)));

//#4 hookup progress-signal from worker to progessParsing-function in this object    
_bRetVal = QObject::connect(_pParseWorker, SIGNAL(progress(int const)), this, SLOT(progressParsing(int const)));

//#5 hookup finished-signal from worker to completedParsing-function in this object
_bRetVal = QObject::connect(_pParseWorker, SIGNAL(finished(int const)), this, SLOT(completedParsing(int const)));

//#6 when worker emits finished-signal, signal thread to quit (shutdown)
_bRetVal = QObject::connect(_pParseWorker, SIGNAL(finished(int const)), _pParseThread, SLOT(quit()));
//#7 when worker emits finished-signal, mark worker to be deleted
_bRetVal = QObject::connect(_pParseWorker, SIGNAL(finished(int const)), _pParseWorker, SLOT(deleteLater()));
//++++ this connect fails at runtime ++++
    //#8 when thread emits finished-signal, mark mapper to be deleted
    _bRetVal = QObject::connect(_pParseThread, SIGNAL(finished(int const)), _pParseMapper, SLOT(deleteLater()));
//+++++++++++++++++++++++++++++++++++++++
//#9 when thread emits finished-signal, mark thread to be deleted
_bRetVal = QObject::connect(_pParseThread, SIGNAL(finished(int const)), _pParseThread, SLOT(deleteLater()));
_pParseThread->start();

The whole code compiles without any errors, but the 8th connect fails at runtime. I thought, that when i create an instance of QSignalMapper ... it has to bee deleted sometime or other. Maybe it's wasted code. I'm not that familiar with Qt.

Any suggestions?

Thanks in advance.

1 Answers1

0

but the 8th connect fails at runtime.

An error message is printed to stdout - what does it print?

btw: The QProcess::finished() signal is SIGNAL(finished(int)) - this might be your problem.

chehrlic
  • 913
  • 1
  • 5
  • 5
  • My project not a console application, so stdout is not visible by default. Is there an GetLastError-equivalent or do i have to read/redirect stdout manually? Is QProcess::finished() inherited by QThread::finished()? I will try to remove the const statemant in SIGNAL(finished(int)). – MagicMove17 Mar 18 '20 at 19:36
  • Thanks for pushing me to the right direction. Changing "connect(_pParseThread, SIGNAL(finished(int const))..." to connect(_pParseThread, SIGNAL(finished())..." solves this issue. The Copy&Paste-daemon got me ;-) because the QThread's-signal is "void finished()" (without arguments/parameters). – MagicMove17 Mar 18 '20 at 19:48
  • You can either set it as console application in your pro file: CONFIG += console Or add a Qt message handler: https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler – chehrlic Mar 18 '20 at 19:55
  • At the end, my project result in a library, which will be integrated into an existing Qt-GUI-application like a plugin. So i'm very restricted according project settings. Qt message handler is also not suitable for my project (Your link says: This function was introduced in Qt 5.0.). Thanks for your thoughts. – MagicMove17 Mar 18 '20 at 21:15
  • There is a similar function available in Qt4: [qInstallMsgHandler](https://doc.qt.io/archives/qt-4.8/qtglobal.html#qInstallMsgHandler). And the console application - you can use it at least for debug purposes - or do you program your library without a test executable? – chehrlic Mar 19 '20 at 17:03
  • Thanks. Your link is close to my target version, but i found the handler ([qInstallMsgHandler](https://docs.huihoo.com/qt/4.5/qtglobal.html#qInstallMsgHandler)) in Qt4.5 as well. I will investigate that. I test my library in the targeted Qt-GUI-app (which is not from us). The vendor of that app, sells/provides a kind of a toolkit/framework with examples, documentation and project templates to develop/integrate such a plugin/library. So everybody can add own functionality/screens to the standard app. That's why i'm restricted in use of the right Qt version (has to match the app's Qt version). – MagicMove17 Mar 20 '20 at 19:54