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 848 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.
  • ABDUA Offline
    ABDUA Offline
    ABDU
    wrote on 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
    3
    • E ekato993

      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.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on 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

        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.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on 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

          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.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on 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
          • ABDUA ABDU

            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 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.
            
            KroMignonK 1 Reply Last reply
            0
            • E ekato993

              @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.
              
              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on 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
              5
              • KroMignonK 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.

                E Offline
                E Offline
                ekato993
                wrote on 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, ***);});
                
                jsulmJ J.HilkJ KroMignonK 3 Replies Last reply
                0
                • E 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, ***);});
                  
                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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

                    @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.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 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

                      @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, ***);});
                      
                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on 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

                      • Login

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