Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved get the return value of the emitted signal

    General and Desktop
    5
    11
    366
    Loading More Posts
    • 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.
    • E
      ekato993 last edited by

      MyLineEdit class

      virtual void keyPressEvent(QKeyEvent* event) override {
           if(event->key() == Qt::Key_A || event->key() == Qt::Key_2) {
               QString keyName = QKeySequence(event->key()).toString();
               emit oneDigit() = keyName;
           } else { event->ignore(); }
       }
      
      signals:
          QString oneDigit();
      

      MainWindow constructor connection

      connect(line, &MyLineEdit::oneDigit, this, [=]{MainWindow::updateLineEdit(line,***)})
      
      void MainWindow::updateLineEdit(MyLineEdit* line, QString str) {
      
          QString txt = line->text();
          line->setText(txt + str);
      
      }
      

      I would like to give the oneDigit signal return value to the updateLineEdit function.

      jsulm J.Hilk KroMignon 3 Replies Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @ekato993 last edited by

        @ekato993 said in get the return value of the emitted signal:

        I know about lambdas but how to pass the string?

        connect(line, &MyLineEdit::KeyOneDigit, this, [=](Qstring){MainWindow::updateLineEdit(line, ***);});
        ...
        emit KeyOneDigit(key);
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • ABDU
          ABDU last edited by

          You could do it like that

          // In MyLineEdit
          signals:
              void oneDigit(QString str);
          // In MainWindow
          public slots:
              void updateLineEdit(QString str)
              {
                  QString text = line->text();
                  line->setText(text + str);
              }
          

          then connect it this way

          // MainWindow constructor
          connect(line, &MyLineEdit::oneDigit, this, &MainWindow::updateLineEdit);
          

          In keyPressEvent send it this way:

          emit oneDigit(keyName);
          
          E 1 Reply Last reply Reply Quote 3
          • jsulm
            jsulm Lifetime Qt Champion @ekato993 last edited by jsulm

            @ekato993 Return values in signals do not make sense. If you want to pass something from signal to slot then simply use normal parameters:

            signals:
                void oneDigit(QString);
            

            Also what do you want to achieve with this line:

            emit oneDigit() = keyName;
            

            ?!
            You are assigning keyName to a temporary string which has absolutely no effect...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 3
            • J.Hilk
              J.Hilk Moderators @ekato993 last edited by

              @ekato993 I was under the impression, that it was not possible period.
              but I found this stack overflow thread:
              https://stackoverflow.com/questions/5842124/can-qt-signals-return-a-value

              Seems like you can get the return value of the slot to the emitter. With a view caveats. Like DirectConnections only and only the last slot connected.

              But, when I understand your question correctly, thats not even your goal right?

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply Reply Quote 0
              • KroMignon
                KroMignon @ekato993 last edited by

                @ekato993 said in get the return value of the emitted signal:

                signals:
                QString oneDigit();

                Please take time to read documentation (cf. https://doc.qt.io/qt-5/signalsandslots.html).
                You will find there:

                Signals are automatically generated by the moc and must not be implemented in the .cpp file. They can never have return types (i.e. use void).

                So you want to do something which is not allowed.

                Please tell us why you think you need to have a return value for a signal.
                I think you have missunderstood how signals/slots mechanism works.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply Reply Quote 3
                • E
                  ekato993 @ABDU last edited by

                  @ABDU

                  That is look like what I want.

                  Missing function argument, so it should look liket this: updateLineEdit(MyLineEdit *line, QString str)

                  But I am getting the following error,

                  C:\Qt\5.12.11\mingw73_64\include\QtCore\qglobal.h:121: error: static assertion failed: The slot requires more arguments than the signal provides.
                  
                  KroMignon 1 Reply Last reply Reply Quote 0
                  • KroMignon
                    KroMignon @ekato993 last edited by KroMignon

                    @ekato993 said in get the return value of the emitted signal:

                    That is look like what I want.
                    Missing function argument, so it should look liket this: updateLineEdit(MyLineEdit *line, QString str)
                    But I am getting the following error,
                    C:\Qt\5.12.11\mingw73_64\include\QtCore\qglobal.h:121: error: static assertion failed: The slot requires more arguments than the signal provides.

                    Why do you don't take time to read documentation to understand signals/slots mechanism?

                    • You can NOT connect a signal to a slot which have less arguments
                    • signal could have more arguments types must match with slots arguments types.

                    If you need to add extra parameters for a slot, you can use lambda for this (cf. https://doc.qt.io/qt-5/signalsandslots.html#advanced-signals-and-slots-usage).

                    But first you should know and explain what you want to do, so we could give you hints how to achieve it.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    E 1 Reply Last reply Reply Quote 5
                    • E
                      ekato993 @KroMignon last edited by ekato993

                      @KroMignon

                      I just want to get the pressed key as a string and add it to the lineedit. Very important is to allow specific key.

                      I know about lambdas but how to pass the string?

                      connect(line, &MyLineEdit::KeyOneDigit, this, [=]{MainWindow::updateLineEdit(line, ***);});
                      
                      jsulm J.Hilk KroMignon 3 Replies Last reply Reply Quote 0
                      • jsulm
                        jsulm Lifetime Qt Champion @ekato993 last edited by

                        @ekato993 said in get the return value of the emitted signal:

                        I know about lambdas but how to pass the string?

                        connect(line, &MyLineEdit::KeyOneDigit, this, [=](Qstring){MainWindow::updateLineEdit(line, ***);});
                        ...
                        emit KeyOneDigit(key);
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply Reply Quote 1
                        • J.Hilk
                          J.Hilk Moderators @ekato993 last edited by

                          @ekato993 said in get the return value of the emitted signal:

                          @KroMignon

                          I just want to get the pressed key as a string and add it to the lineedit. Very important is to allow specific key.

                          alright,

                          make the following changes:

                          signals:
                              void oneDigit(QString digit); // your signal now as an argument 
                          
                          emit oneDigit(keyName); // your emit the signal with the argument
                          
                          connect(line, &MyLineEdit::oneDigit, [line](QString key)->void{line->setText(line->text() + key);});
                          

                          No guarantee the lambda/connect is free of typos :D

                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply Reply Quote 1
                          • KroMignon
                            KroMignon @ekato993 last edited by

                            @ekato993 said in get the return value of the emitted signal:

                            I know about lambdas but how to pass the string?

                            Perhaps you are aware about lambdas but that is not the question here.
                            You have to learn signals/slots usage, because it is a very important concept in Qt world.
                            So if you want to build applications with Qt, take time to learn:

                            • what QObject are => https://doc.qt.io/qt-5/object.html
                            • what QObject parent <=> child relation chip => https://doc.qt.io/qt-5/objecttrees.html
                            • how signals/slots works => https://doc.qt.io/qt-5/signalsandslots.html

                            Those are the minimum to be comfortable with Qt development.
                            This will save you many hours of frustration and headaches!

                            It is up to you to learn, the documentation is freely available.

                            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                            1 Reply Last reply Reply Quote 2
                            • First post
                              Last post