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. get the return value of the emitted signal
Forum Updated to NodeBB v4.3 + New Features

get the return value of the emitted signal

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 5 Posters 799 Views 1 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.
  • E Offline
    E Offline
    ekato993
    wrote on 22 Jul 2021, 23:43 last edited by
    #1

    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.

    J J K 3 Replies Last reply 23 Jul 2021, 05:20
    0
    • E ekato993
      23 Jul 2021, 08:55

      @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, ***);});
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 23 Jul 2021, 09:05 last edited by
      #9

      @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
      1
      • A Offline
        A Offline
        ABDU
        wrote on 23 Jul 2021, 04:48 last edited by
        #2

        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 23 Jul 2021, 08:05
        3
        • E ekato993
          22 Jul 2021, 23:43

          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.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 23 Jul 2021, 05:20 last edited by jsulm
          #3

          @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
          3
          • E ekato993
            22 Jul 2021, 23:43

            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.

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 23 Jul 2021, 05:36 last edited by
            #4

            @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


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

            1 Reply Last reply
            0
            • E ekato993
              22 Jul 2021, 23:43

              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.

              K Offline
              K Offline
              KroMignon
              wrote on 23 Jul 2021, 05:43 last edited by
              #5

              @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
              3
              • A ABDU
                23 Jul 2021, 04:48

                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 Offline
                E Offline
                ekato993
                wrote on 23 Jul 2021, 08:05 last edited by
                #6

                @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.
                
                K 1 Reply Last reply 23 Jul 2021, 08:11
                0
                • E ekato993
                  23 Jul 2021, 08:05

                  @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.
                  
                  K Offline
                  K Offline
                  KroMignon
                  wrote on 23 Jul 2021, 08:11 last edited by KroMignon
                  #7

                  @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 23 Jul 2021, 08:55
                  5
                  • K KroMignon
                    23 Jul 2021, 08:11

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

                    E Offline
                    E Offline
                    ekato993
                    wrote on 23 Jul 2021, 08:55 last edited by ekato993
                    #8

                    @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, ***);});
                    
                    J J K 3 Replies Last reply 23 Jul 2021, 09:05
                    0
                    • E ekato993
                      23 Jul 2021, 08:55

                      @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, ***);});
                      
                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 23 Jul 2021, 09:05 last edited by
                      #9

                      @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
                      1
                      • E ekato993
                        23 Jul 2021, 08:55

                        @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, ***);});
                        
                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 23 Jul 2021, 09:05 last edited by
                        #10

                        @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


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

                        1 Reply Last reply
                        1
                        • E ekato993
                          23 Jul 2021, 08:55

                          @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, ***);});
                          
                          K Offline
                          K Offline
                          KroMignon
                          wrote on 23 Jul 2021, 09:12 last edited by
                          #11

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

                          2/11

                          23 Jul 2021, 04:48

                          9 unread
                          • Login

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