Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connect problem

Connect problem

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.5k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ofmrew
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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.

      O 1 Reply Last reply
      2
      • ? A Former User

        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.

        O Offline
        O Offline
        ofmrew
        wrote on last edited by
        #3

        @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.

        O 1 Reply Last reply
        0
        • O ofmrew

          @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.

          O Offline
          O Offline
          ofmrew
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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.

            O 1 Reply Last reply
            2
            • ? A Former User

              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.

              O Offline
              O Offline
              ofmrew
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                O 1 Reply Last reply
                0
                • SGaistS SGaist

                  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.

                  O Offline
                  O Offline
                  ofmrew
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved