[Interest] QObject::connect returns true, but the, connection does not work

Ch'Gans chgans at gna.org
Sat Feb 25 21:41:36 CET 2017


On 26 February 2017 at 06:09, Roland Hughes <roland at logikalsolutions.com> wrote:
> Stupid question, but is this a console app?
>
> I'm asking because this happens with console apps. It can happen with any
> app, but console apps tend to have the problem most often. Here is what I
> mean:
>
> #include <QCoreApplication>
> #include <QDebug>
> #include <QTimer>
>
> #include "consoleinput.h"
>
> int main(int argc, char *argv[])
> {
>     QCoreApplication a(argc, argv);
>     ConsoleInput console;
>     bool b = a.connect(&console, &ConsoleInput::quit, &a,
> &QCoreApplication::quit);
>     qDebug() << "result of connect " << b;
>
>     QTimer::singleShot(0, &console, &ConsoleInput::run);
>
>     return a.exec();
>
> }
>
> If you do not use a QTimer to launch the console input method so it runs
> from within the exec() loop, i.e. replace the QTimer line with
>
> console.run();
>
> Your connect call will return true but the signal will never trigger the
> slot because execution is occurring outside of an event loop.

connect() returns true, simply means that the connection has been
made, if your concole.run() doesn't run an event loop no signal/slot
will work AFAIK.

Put a break point in ConsoleInput::quit() and check the call stack
with the debugger to understand how this works.

Chris

>
> --
>
> Roland Hughes, President
> Logikal Solutions
>
> http://www.theminimumyouneedtoknow.com
> http://www.infiniteexposure.net
> http://www.johnsmith-book.com
> http://www.logikalblog.com
> http://www.interestingauthors.com/blog
> http://lesedi.us/
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list