QScopedPointer object, signal/slot usage, I am confused.
-
Re: Hybrid Qt GUI/Console cmake application on Windows
Referring to the above topic of mine.
Have extended the code to this:int main(int argc, char *argv[]) { int exitStatus = EXIT_SUCCESS; QScopedPointer<QCoreApplication> app( createApplication(argc, argv) ); // if cast to QApp is successful => GUI mode was enabled if (qobject_cast<QApplication *>(app.data())) { //ShowWindow(GetConsoleWindow(), SW_HIDE); // start GUI version... MainWindow w; w.show(); exitStatus = app->exec(); } else { // start non-GUI version... // QStringList args = QCoreApplication::arguments(); // parseArgs(args); BackendWorker worker; // Quit application when work is finished QObject::connect(&worker, &BackendWorker::finished, app.data(), &QCoreApplication::exit); // [ 1 ] // QObject::connect(&worker, &BackendWorker::finished, app, &QCoreApplication::exit); // [ 2 ] QTimer::singleShot(0, &worker, &BackendWorker::start); exitStatus = app->exec(); // [ 3 ] // exitStatus = app.data()->exec(); // [ 4 ] } return exitStatus; }
I'm confused about the connection part where
[ 1 ]
works and[ 2 ]
does not.
Whereas both of[ 3 ]
and[ 4 ]
work and do the same.
And this is why it confuses me a little.Uncommenting
[ 2 ]
throws:error: no matching function for call to 'QObject::connect(BackendWorker *, void (BackendWorker::*)(int),
QScopedPointer<QCoreApplication>&, void (*)(int))'69 | QObject::connect(&worker, &BackendWorker::finished, app, &QCoreApplication::exit);
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Typing
app->...
even auto-completes/suggests functions likeexec()
,quit()
orexit()
(all part ofQCoreApplication
API)
I know these suggestions mean nothing :)But if you are able to call
QCoreApplication::exec
onQScopedPointer<QCoreApplication> app
as in[ 3 ]
, why you can't connect a signal like that (as in[ 2 ]
) ? -
3 and 4 are the same because of this operator overload: https://doc.qt.io/qt-6/qscopedpointer.html#operator--gt
-
@Christian-Ehrlicher said in QScopedPointer object, signal/slot usage, I am confused.:
3 and 4 are the same because of this operator overload: https://doc.qt.io/qt-6/qscopedpointer.html#operator--gt
lol, thank you, totally missed that.
Didn't even know that->
is an operator which you can overload :o
Same with.
- operator (as I'm seeing it now) -
-
You can do nasty things with this. Overload operator + for integers to surprise your colleagues for example 😁