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. QList / QPushbutton and connect/slot features

QList / QPushbutton and connect/slot features

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 2.3k Views
  • 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.
  • jipe3001J Offline
    jipe3001J Offline
    jipe3001
    wrote on last edited by
    #1

    I have recently created a list of QPushbuttons as follows:

    H file:
    QList < QList <QPushButton *>> buttons_per_session;
    QList < QPushButton *> buttons_per_tab;

    Cpp file:

         for (qint16 i=0; i <= total; i++)
                       {
                            buttons_per_tab.append(new QPushButton);
                       }
    
         grid_per_session.append(grid_per_tab);
    

    and everything is working fine

    but now I would like to connect these buttons to an action as follows:

    connect(buttons_per_session[x][y], SIGNAL(clicked()),this, SLOT(changeColor()));

    My concern is how can I get x & y (QList indices) from a clicked button?

    Thks for your usual coop

    J.HilkJ 1 Reply Last reply
    0
    • jipe3001J Offline
      jipe3001J Offline
      jipe3001
      wrote on last edited by
      #13

      J.Hilk

      I used your last solution ans it's working fine

      Thks for your help

      1 Reply Last reply
      0
      • KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #2

        in changeColor() slot, you can use sender() to get the QPushButton instance which generates to signal.

        You can do something like this:

        for(int idx = 0; idx < grid_per_session.size(); ++idx)
        {
              int pos = grid_per_session.at(idx).indexOf(sender());
             if (pos >=0 )
             {
                 // found !
             }
        }
        

        Regards

        Fabrice

        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
        • jipe3001J jipe3001

          I have recently created a list of QPushbuttons as follows:

          H file:
          QList < QList <QPushButton *>> buttons_per_session;
          QList < QPushButton *> buttons_per_tab;

          Cpp file:

               for (qint16 i=0; i <= total; i++)
                             {
                                  buttons_per_tab.append(new QPushButton);
                             }
          
               grid_per_session.append(grid_per_tab);
          

          and everything is working fine

          but now I would like to connect these buttons to an action as follows:

          connect(buttons_per_session[x][y], SIGNAL(clicked()),this, SLOT(changeColor()));

          My concern is how can I get x & y (QList indices) from a clicked button?

          Thks for your usual coop

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #3

          hi @jipe3001

          do you really need that indices after you populated the list? why don't you make the connect during QPushButton creation ?

           for (qint16 i=0; i <= total; i++)
                             {
                                 QPushButton *btn = new QPushButton();
                                 connect(btn, SIGNAL(clicked()),this, SLOT(changeColor()));
                                  buttons_per_tab.append(new QPushButton);
                             }
          
               grid_per_session.append(grid_per_tab);
          
          

          or if you really want to do this afterwards:

          for(List<QPushButton *> &list : buttons_per_tab)
                for (QPushButton *btn : list)
                       connect(btn, SIGNAL(clicked()),this, SLOT(changeColor()));
          

          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
          2
          • jipe3001J Offline
            jipe3001J Offline
            jipe3001
            wrote on last edited by
            #4

            CORRECTED VERSION

            I have recently created a list of QPushbuttons as follows:

            H file:
            QList < QList <QPushButton *>> buttons_per_session;
            QList < QPushButton *> buttons_per_tab;

            Cpp file:

             for (qint16 i=0; i <= total; i++)
                           {
                                buttons_per_tab.append(new QPushButton);
                           }
            
             buttons_per_session.append(buttons_per_tab);
            

            and everything is working fine

            but now I would like to connect these buttons to an action as follows:

                       connect(buttons_per_session[x][y], SIGNAL(clicked()),this, SLOT(changeColor()));
            

            My concern is how can I get x & y (QList indices) from a clicked button?

            Thks for your usual coop

            KroMignonK 1 Reply Last reply
            0
            • jipe3001J jipe3001

              CORRECTED VERSION

              I have recently created a list of QPushbuttons as follows:

              H file:
              QList < QList <QPushButton *>> buttons_per_session;
              QList < QPushButton *> buttons_per_tab;

              Cpp file:

               for (qint16 i=0; i <= total; i++)
                             {
                                  buttons_per_tab.append(new QPushButton);
                             }
              
               buttons_per_session.append(buttons_per_tab);
              

              and everything is working fine

              but now I would like to connect these buttons to an action as follows:

                         connect(buttons_per_session[x][y], SIGNAL(clicked()),this, SLOT(changeColor()));
              

              My concern is how can I get x & y (QList indices) from a clicked button?

              Thks for your usual coop

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by KroMignon
              #5

              @jipe3001: you want to get (X,Y) in the called slot? Right?
              So you can use sender() to got the instance which emit the signal

              IMPORTANT: to be able to use sender() you must force QueuedConnection!

              ...
              connect(buttons_per_session[x][y], &QPushButton::clicked, this, &MyClass::changeColor, Qt::QueuedConnection);
              
              ...
              void MyClass::changeColor()
              {
                  auto* btn =qobject_cast<QPushButton*>(sender());
                  if (!btn)
                      return;
              
                  for(int idx = 0; idx < grid_per_session.size(); ++idx)
                  {
                       int pos = grid_per_session.at(idx).indexOf(btn);
                       if (pos >=0 )
                       {
                           // sender() == grid_per_session[idx][pos]
                       }
                  }
              }
              

              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
              0
              • J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                I may have miss read your question.

                I would however discourage the use of sender() as it n violates the object-oriented principle of modularity.

                The recommended way is to use QSignalMapper

                But since Qt5 also supports the use of lambdas inside connects, that is also a valid option:

                connect(btn, &QPushButton::clicked,this, [=]()->void{changeColor(i,j);});
                

                with i and j as parameter of your slot.


                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
                3
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @KroMignon said in QList / QPushbutton and connect/slot features:

                  IMPORTANT: to be able to use sender() you must force QueuedConnection!

                  That's completely wrong. Why would you need that ?

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

                  KroMignonK 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    @KroMignon said in QList / QPushbutton and connect/slot features:

                    IMPORTANT: to be able to use sender() you must force QueuedConnection!

                    That's completely wrong. Why would you need that ?

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #8

                    @SGaist , not completely wrong, just a little bit ;-)
                    In fact, the only thing to carry on, is that DirectConnection is not used when the slot is not in same thread as emitter.
                    So forcing QueuedConnection will ensure this will never happen.

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

                    SGaistS 1 Reply Last reply
                    0
                    • KroMignonK KroMignon

                      @SGaist , not completely wrong, just a little bit ;-)
                      In fact, the only thing to carry on, is that DirectConnection is not used when the slot is not in same thread as emitter.
                      So forcing QueuedConnection will ensure this will never happen.

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @KroMignon said in QList / QPushbutton and connect/slot features:

                      @SGaist , not completely wrong, just a little bit ;-)

                      No, it is completely wrong, sender's usage does not depend on the type of connection.

                      In fact, the only thing to carry on, is that DirectConnection is not used when the slot is not in same thread as emitter.

                      That part is correct.

                      So forcing QueuedConnection will ensure this will never happen.

                      And why would you need to force that for everything ?

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

                      KroMignonK 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        @KroMignon said in QList / QPushbutton and connect/slot features:

                        @SGaist , not completely wrong, just a little bit ;-)

                        No, it is completely wrong, sender's usage does not depend on the type of connection.

                        In fact, the only thing to carry on, is that DirectConnection is not used when the slot is not in same thread as emitter.

                        That part is correct.

                        So forcing QueuedConnection will ensure this will never happen.

                        And why would you need to force that for everything ?

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #10

                        @SGaist said in QList / QPushbutton and connect/slot features:

                        No, it is completely wrong, sender's usage does not depend on the type of connection.

                        Okay, your right, QueuedConnection is not required. My mistake.

                        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
                        0
                        • jipe3001J Offline
                          jipe3001J Offline
                          jipe3001
                          wrote on last edited by
                          #11

                          Thks for all your answers but I am getting a bit confused with the various solutions suggested.

                          To be clear :

                          I am creating a various list of QPushbutttons using this structure:

                                   QList < QList <QPushButton *>> buttons_per_session;
                                   QList < QPushButton *> buttons_per_tab;
                          

                          I therefore is interested in creating an action for each QPushbutton

                          Of course I can generate a connect statement when creating each QPushButton but
                          this may end with hundreds of connect lines (number not known and dependent of the opened file)
                          which does not seem to be very elegant and memory consuming.

                          To avoid this I am looking for a solution for which I can determine which QPushButton has been clicked
                          and send these parameters to the slot.

                          The solution below seems to be fine to send the parameters (i,j) to the changeColor slot
                          .
                          connect(btn, &QPushButton::clicked,this, =->void{changeColor(i,j);});

                          but I cant figure out how these parameters are determined, btn does not seems to be part of my QList structure

                          Tks for your support

                          J.HilkJ 1 Reply Last reply
                          0
                          • jipe3001J jipe3001

                            Thks for all your answers but I am getting a bit confused with the various solutions suggested.

                            To be clear :

                            I am creating a various list of QPushbutttons using this structure:

                                     QList < QList <QPushButton *>> buttons_per_session;
                                     QList < QPushButton *> buttons_per_tab;
                            

                            I therefore is interested in creating an action for each QPushbutton

                            Of course I can generate a connect statement when creating each QPushButton but
                            this may end with hundreds of connect lines (number not known and dependent of the opened file)
                            which does not seem to be very elegant and memory consuming.

                            To avoid this I am looking for a solution for which I can determine which QPushButton has been clicked
                            and send these parameters to the slot.

                            The solution below seems to be fine to send the parameters (i,j) to the changeColor slot
                            .
                            connect(btn, &QPushButton::clicked,this, =->void{changeColor(i,j);});

                            but I cant figure out how these parameters are determined, btn does not seems to be part of my QList structure

                            Tks for your support

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by J.Hilk
                            #12

                            @jipe3001

                            untested, beware of typos.

                            QList < QList <QPushButton *>> buttons_per_session;
                            for( int i(0); i< 10; i ++) {
                                  QList < QPushButton *> buttons_per_tab;
                                  for(int j(0); j < 10; j++) {
                                          QPushButton *btn = new QPushButton();
                                          connect(btn, &QPushButton::clicked,this, [=]()->void{changeColor(i,j);});
                                          buttons_per_tab.append(btn);
                                  }
                                  buttons_per_session.append(buttons_per_tab);
                                  
                            }
                                     
                            

                            [edit: fixed small mistake SGaist]


                            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
                            2
                            • jipe3001J Offline
                              jipe3001J Offline
                              jipe3001
                              wrote on last edited by
                              #13

                              J.Hilk

                              I used your last solution ans it's working fine

                              Thks 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