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. Question about signals
Forum Updated to NodeBB v4.3 + New Features

Question about signals

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.0k 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.
  • I Offline
    I Offline
    Inzinejkr
    wrote on last edited by
    #1

    I'm creating a few check boxes in a loop and connecting them to a lambda functon like this:

    connect(cbtn, &QCheckBox::clicked, this, [=](){funct(something);});
                    
    

    and I need to know which state the check box is in (checked or not) in the function. Is there a way to find this out with the QObject::sender()?

    Pl45m4P 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      just pass the checkbox as a second argument to funct for example, if the signature is void MyClass::funct(QString); change it to void MyClass::funct(QString,QCheckBox*); and chenge the connect to QObject::connect(cbtn, &QCheckBox::clicked, this, std::bind(&MyClass::funct,std::placeholders::_1,cbtn)); or QObject:connect(cbtn, &QCheckBox::clicked, this, [=](){funct(something,cbtn);});

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • I Inzinejkr

        I'm creating a few check boxes in a loop and connecting them to a lambda functon like this:

        connect(cbtn, &QCheckBox::clicked, this, [=](){funct(something);});
                        
        

        and I need to know which state the check box is in (checked or not) in the function. Is there a way to find this out with the QObject::sender()?

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @Inzinejkr

        Why not

        connect(cbtn, &QCheckBox::stateChanged, this, [=](int state){ fnct(something, state); });
        

        https://doc.qt.io/qt-5/qcheckbox.html#stateChanged

        In addition you can pass also the sender, like @VRonin wrote here

        @VRonin said in Question about signals:

        QObject:connect(cbtn, &QCheckBox::clicked, this, [=]{funct(something,cbtn);});


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Inzinejkr
          wrote on last edited by
          #4

          The thing is, I'm using the same function for connecting radio buttons and line edits. How would that work?

          Pl45m4P 1 Reply Last reply
          0
          • I Inzinejkr

            The thing is, I'm using the same function for connecting radio buttons and line edits. How would that work?

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @Inzinejkr

            Use QWidget or QObject as parameter in fnct instead of QCheckBox, then cast it to sender type.

            @Inzinejkr said in Question about signals:

            I'm using the same function for connecting radio buttons and line edits. How would that work?

            The whole idea seems weird. What does your function do? Why do you want to use the same connection for QLineEdit type widgets and different button types? They have completely different behaviors and functions.
            QLineEdit also has no clicked signal, unless you have some custom type QLineEdit subclasses.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            2
            • JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              There are two voters here for choosing to pass the sending object --- QCheckBox* or whatever --- as a parameter to the signal/slot. I will just observe that all (at least widget) Qt-supplied signals/slots have chosen not to do this --- although they easily could have --- and instead pass a parameter for, say, the checked state or the line edit's string. Make of that what you will....

              @Inzinejkr
              As @Pl45m4 has said: there may be some commonality in, say, checkbox or radiobutton clicking events, e.g. on/off state. It is harder to see where that would be shared with line edit clicking. Perhaps the line edits would be better with a different slot?

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Inzinejkr
                wrote on last edited by
                #7

                The function just takes the text() of whatever is clicked (QLineEdit::editingFinished, QCheckBox::clicked, QRadioButton::clicked) and stores it in a database.. But, currently I have no way of knowing whether the user toggled on or off the check box.. I assumed there would be some easy way without editing all the code..

                JonBJ 1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  use QObject::sender() and try to cast it to a QCheckBox

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  I 1 Reply Last reply
                  0
                  • I Inzinejkr

                    The function just takes the text() of whatever is clicked (QLineEdit::editingFinished, QCheckBox::clicked, QRadioButton::clicked) and stores it in a database.. But, currently I have no way of knowing whether the user toggled on or off the check box.. I assumed there would be some easy way without editing all the code..

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @Inzinejkr
                    Here is one way, a touch different:

                    connect(checkbox, &QCheckBox::clicked, this, [=](bool checked = false) { if (checked) saveText(checkbox->text());} );
                    connect(radiobutton, &QRadioButton::clicked, this, [=](bool checked = false) { if (checked) saveText(radiobutton->text());} );
                    connect(lineedit, &QLineEdit::editingFinished, this, [=]() { saveText(lineEdit->text());} );
                    

                    There are pros & cons to this approach versus passing the widget through the signal/slot.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      use QObject::sender() and try to cast it to a QCheckBox

                      I Offline
                      I Offline
                      Inzinejkr
                      wrote on last edited by
                      #10

                      @Christian-Ehrlicher said in Question about signals:

                      use QObject::sender() and try to cast it to a QCheckBox

                      I just get QCheckBox(0x1f8e510)?

                      @JonB said in Question about signals:

                      @Inzinejkr
                      Here is one way, a touch different:

                      connect(checkbox, &QCheckBox::clicked, this, [=](bool checked = false) { if (checked) saveText(checkbox->text());} );
                      

                      The thing is, I always have to know a state change and what that state is, not just for checked.. Guess I'll just make a new function for the check boxes..

                      Christian EhrlicherC JonBJ 2 Replies Last reply
                      0
                      • I Inzinejkr

                        @Christian-Ehrlicher said in Question about signals:

                        use QObject::sender() and try to cast it to a QCheckBox

                        I just get QCheckBox(0x1f8e510)?

                        @JonB said in Question about signals:

                        @Inzinejkr
                        Here is one way, a touch different:

                        connect(checkbox, &QCheckBox::clicked, this, [=](bool checked = false) { if (checked) saveText(checkbox->text());} );
                        

                        The thing is, I always have to know a state change and what that state is, not just for checked.. Guess I'll just make a new function for the check boxes..

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Inzinejkr said in Question about signals:

                        use QObject::sender() and try to cast it to a QCheckBox

                        I just get QCheckBox(0x1f8e510)?

                        And you want to tell me what with this?
                        You wanted access the the QCheckBox, you have it now - so what's the problem?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • I Inzinejkr

                          @Christian-Ehrlicher said in Question about signals:

                          use QObject::sender() and try to cast it to a QCheckBox

                          I just get QCheckBox(0x1f8e510)?

                          @JonB said in Question about signals:

                          @Inzinejkr
                          Here is one way, a touch different:

                          connect(checkbox, &QCheckBox::clicked, this, [=](bool checked = false) { if (checked) saveText(checkbox->text());} );
                          

                          The thing is, I always have to know a state change and what that state is, not just for checked.. Guess I'll just make a new function for the check boxes..

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @Inzinejkr said in Question about signals:

                          @Christian-Ehrlicher said in Question about signals:

                          use QObject::sender() and try to cast it to a QCheckBox

                          I just get QCheckBox(0x1f8e510)?

                          He wants you to go:

                          QCheckBox *checkbox = qobject_cast<QCheckBox *>(QObject::sender());
                          if (checkbox != nullptr)
                          {
                              if (checkbox->checked())
                                  saveText(checkbox->text());
                          }
                          

                          Read up qobject_cast<>().

                          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