Connect problem



  • I have the following:

    In MainWindow

    connect(parser, &FormulaParser::parseMessage(QString), ui->statusBar, &QStatusBar::showMessage(QString));
    

    In FormulaParser header

    signals:
    void parseMessage(QString);

    In FormulaParser source

    emit parseMessage("No Arrow");

    It shows the message primary-expression before the ) after each QString.

    What am I not seeing?


  • Moderators

    Hi! The second and fourth arguments have to be function addresses, so the correct line would be:

    connect(parser, &FormulaParser::parseMessage, ui->statusBar, &QStatusBar::showMessage);
    

    Se also New Signal Slot Syntax in Qt 5.



  • @Wieland

    Thanks. I know I tried this before, in fact it was what I had on my first attempt, and got a Meta Object error and then I tried nearly every combination and permutation, with no results. I have used signal and slot in multiple programs with no problem but never with an argument being passed. However, a new set of eyes always works.

    Do I assume that I never need to supply the object type in the connect signal and slot, that the compiler will just match the emitted signal to the proper slot? That what the document you reference seems to imply. I had read that document before but never got that from it, but now that I reread it it makes sense.

    Again thanks.



  • @ofmrew

    I found the real problem! The signal statement is incorrect, it should be:

    signals:
    void parseMessage(QString, int);

    Apparently, while you can omit the int when used as a function, namely:

    ui->statusBar->showMessage(QString("Compiling. . ."));

    It appears that the int is required in the signal to match the slot in QStatusBar. I have never seen that requirement stated in the documentation. If what I assume to be the fact, then maybe the documentation should state that it is a requirement?


  • Moderators

    From QStatusBar Class:

    void QStatusBar::showMessage(const QString &message, int timeout = 0)

    timeout has a default argument. The wiki page actually says that the new syntax doesn't support default arguments of slots:

    Default arguments in slot

    If you have code like this:

    class A : public QObject {
         Q_OBJECT
         public slots:
             void someSlot(int foo = 0);
     };
    

    The old method allows you to connect that slot to a signal that does not have arguments.
    But I cannot know with template code if a function has default arguments or not.
    So this feature is disabled.



  • @Wieland

    I found the referenced default arguments in the wiki post; however, that was not my issue. My issue was the the my signal arguments did not match the slot arguments; it had the first non-default but not the argument that was the default argument--the signal needed two arguments. That is what your reference says indirectly. I wish the Qt (not Wiki) document would have stated "the arguments of the signal must be the same as the slot, including any default arguments."

    I am sure that I am not the first to have this problem and surely I will not be the last, maybe this discussion will help some poor soul from hours of frustration.


  • Lifetime Qt Champion

    Hi,

    The signatures of the signals and slots are mentioned just under the graphics in the Signals And Slots chapter of Qt's documentation. And the default argument is a bit lower in the same page.



  • @SGaist

    You are absolutely correct. I just missed it. It was the "primary-expression before" error message that sent me off looking in the wrong direction: I thought it was a mistake in the connect statement and not the signal statement. I only looked at that after I had made my post. I had changed the signal statement to include the defaulted int argument. It was that change that made your correction work. The signal statement in the original post did not work, I tried it. Only the corrected signal statement worked.

    Thanks for your help.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.