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. i cannot check my QPushbutton that is in my qtableview
Forum Updated to NodeBB v4.3 + New Features

i cannot check my QPushbutton that is in my qtableview

Scheduled Pinned Locked Moved Unsolved General and Desktop
32 Posts 3 Posters 3.9k 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.
  • A aftalib

    so C++ lambda might be the way to relate to each of the QPushButtons i created with my loop ?

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

    @aftalib
    You will need the lambda when you are ready to have the single slot code method handle the clicking of any of the push buttons, which I suspect is what you will want to achieve.

    However, before you do that, put a qDebug() << "clicked" in as the first statement in the slot. Verify that gets called when any of the buttons you created are clicked, not just one button. Only at that point are you ready to move to a lambda for the slot which will pass which button was pressed as a parameter to the slot....

    A 1 Reply Last reply
    1
    • JonBJ JonB

      @aftalib
      You will need the lambda when you are ready to have the single slot code method handle the clicking of any of the push buttons, which I suspect is what you will want to achieve.

      However, before you do that, put a qDebug() << "clicked" in as the first statement in the slot. Verify that gets called when any of the buttons you created are clicked, not just one button. Only at that point are you ready to move to a lambda for the slot which will pass which button was pressed as a parameter to the slot....

      A Offline
      A Offline
      aftalib
      wrote on last edited by aftalib
      #13

      @JonB okay, the slot gets called of any of the buttons i have created, i have the "clicked" output on each of the buttons

      JonBJ 1 Reply Last reply
      0
      • A aftalib

        @JonB okay, the slot gets called of any of the buttons i have created, i have the "clicked" output on each of the buttons

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #14

        @aftalib
        And have you used the new style syntax I referred you to earlier? Show your connect() statement now.

        A 1 Reply Last reply
        0
        • JonBJ JonB

          @aftalib
          And have you used the new style syntax I referred you to earlier? Show your connect() statement now.

          A Offline
          A Offline
          aftalib
          wrote on last edited by
          #15

          @JonB i dont think i got it right.. the IDE still says that clicked() is an undeclared identifier.. 5d0c7d30-3cdf-4f64-a2c0-8429bc0d725f-image.png

          JonBJ jsulmJ 2 Replies Last reply
          0
          • A aftalib

            @JonB i dont think i got it right.. the IDE still says that clicked() is an undeclared identifier.. 5d0c7d30-3cdf-4f64-a2c0-8429bc0d725f-image.png

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #16

            @aftalib
            I said to you earlier:

            You have to read and act on https://wiki.qt.io/New_Signal_Slot_Syntax.

            but you do not seem to have done anything about this....

            A 1 Reply Last reply
            1
            • JonBJ JonB

              @aftalib
              I said to you earlier:

              You have to read and act on https://wiki.qt.io/New_Signal_Slot_Syntax.

              but you do not seem to have done anything about this....

              A Offline
              A Offline
              aftalib
              wrote on last edited by
              #17

              @JonB no i did check this in order to do the new connect statement but i dont get how am i supposed to write this with what is written on this page

              JonBJ 1 Reply Last reply
              0
              • A aftalib

                @JonB i dont think i got it right.. the IDE still says that clicked() is an undeclared identifier.. 5d0c7d30-3cdf-4f64-a2c0-8429bc0d725f-image.png

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #18

                @aftalib Please post text not screen shots.

                connect(H11, &QPushButton::clicked, this, &WHAT_EVER_THIS_TYPE_IS::on_H11_clicked);
                

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

                1 Reply Last reply
                3
                • A aftalib

                  @JonB no i did check this in order to do the new connect statement but i dont get how am i supposed to write this with what is written on this page

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #19

                  @aftalib
                  The very first section there shows an example of changing from SIGNAL/SLOT() macros

                  connect(
                      sender, SIGNAL( valueChanged( QString, QString ) ),
                      receiver, SLOT( updateValue( QString ) )
                  );
                  

                  over to

                  connect(
                      sender, &Sender::valueChanged,
                      receiver, &Receiver::updateValue
                  );
                  

                  The code you have pasted does not attempt to follow that required pattern.

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    aftalib
                    wrote on last edited by
                    #20

                    okay thats my bad i didnt quite understand the syntax that was written in the new connect statement but i think i got it now, thank you. here's what i wrote :

                             connect(H11, &QPushButton::clicked,this,&AntArrayDialog::on_H11_clicked);
                    
                    

                    i dont get any errors and the program launches well

                    JonBJ 1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      aftalib
                      wrote on last edited by
                      #21

                      then i have to use lambda with this syntax in order for my slot to work with any of the buttons i created right?

                      1 Reply Last reply
                      0
                      • A aftalib

                        okay thats my bad i didnt quite understand the syntax that was written in the new connect statement but i think i got it now, thank you. here's what i wrote :

                                 connect(H11, &QPushButton::clicked,this,&AntArrayDialog::on_H11_clicked);
                        
                        

                        i dont get any errors and the program launches well

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #22

                        @aftalib
                        Yes, this is good :)

                        Now, I believe, since (I assume) you will have each of the buttons connect to this one slot method, you will want the slot to receive a parameter to tell it which particular button was clicked this time to call the slot, right?

                        To do that you are going to need to move on to connect()ing a C++ lambda instead of the slot function as you currently do now. Then a parameter can be added, so that the signal will pass it to the slot. the parameter will identify which button emitted the signal.

                        That new-style syntax page includes the following example:

                        Can be used with C++11 lambda expressions:

                        connect(
                            sender, &Sender::valueChanged,
                            [=]( const QString &newValue ) { receiver->updateValue( "senderValue", newValue ); }
                        );
                        

                        The lambda bit is:

                        [...](...) { ... }
                        

                        See if you can figure how to pass the QPushButton * object from the signalling push button to the slot....

                        A 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @aftalib
                          Yes, this is good :)

                          Now, I believe, since (I assume) you will have each of the buttons connect to this one slot method, you will want the slot to receive a parameter to tell it which particular button was clicked this time to call the slot, right?

                          To do that you are going to need to move on to connect()ing a C++ lambda instead of the slot function as you currently do now. Then a parameter can be added, so that the signal will pass it to the slot. the parameter will identify which button emitted the signal.

                          That new-style syntax page includes the following example:

                          Can be used with C++11 lambda expressions:

                          connect(
                              sender, &Sender::valueChanged,
                              [=]( const QString &newValue ) { receiver->updateValue( "senderValue", newValue ); }
                          );
                          

                          The lambda bit is:

                          [...](...) { ... }
                          

                          See if you can figure how to pass the QPushButton * object from the signalling push button to the slot....

                          A Offline
                          A Offline
                          aftalib
                          wrote on last edited by aftalib
                          #23

                          @JonB okay, just before you answered i tried this :

                          connect(H11, &QPushButton::clicked,[this](){
                          
                                       if (H11->isChecked()) {
                          
                                           H11->setText("H2");
                                           qDebug() << "test";
                                       }
                          
                                       else if (H11->isChecked()==false){
                                           H11->setText("H1");
                          
                          
                                       }
                          
                                   });
                          

                          so if i click on any button i want it to change to H2, then to H1 if i reclick on it and so on..
                          i didnt get any errors but it didnt work so im assuming this is wrong. im gonna retry something else with what you said

                          JonBJ 1 Reply Last reply
                          0
                          • A aftalib

                            @JonB okay, just before you answered i tried this :

                            connect(H11, &QPushButton::clicked,[this](){
                            
                                         if (H11->isChecked()) {
                            
                                             H11->setText("H2");
                                             qDebug() << "test";
                                         }
                            
                                         else if (H11->isChecked()==false){
                                             H11->setText("H1");
                            
                            
                                         }
                            
                                     });
                            

                            so if i click on any button i want it to change to H2, then to H1 if i reclick on it and so on..
                            i didnt get any errors but it didnt work so im assuming this is wrong. im gonna retry something else with what you said

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #24

                            @aftalib
                            At least you have you lambda syntax correct, that is good!

                            Your connect() is not attempting to pass which button was clicked, as a parameter to the slot. The slot only references whatever one button this->H11 currently refers to when it is executed. If you are wanting the slot to know which button invoked it in order to do its work, you are going to need that as a parameter to the slot in the lambda.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              aftalib
                              wrote on last edited by
                              #25

                              @JonB said in i cannot check my QPushbutton that is in my qtableview:

                              okay i get what you're saying, so i need to put the button that is calling the slot as a parameter. here's what i tried but its not working either :

                               connect(H11, &QPushButton::clicked,[=](QPushButton &button){
                              
                                           if (button.isChecked()) {
                              
                                               button.setText("H2");
                                               qDebug() << "test";
                                           }
                              
                                           else if (button.isChecked()==false){
                                              button.setText("H1");
                              
                              
                                           }
                              
                                       });
                              

                              i know that its almost the right thing but im missing something

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                aftalib
                                wrote on last edited by
                                #26

                                so i've spent most of the last night searching for a solution to this but still havent found. does anyone have an idea about this ?

                                thank you in advance

                                JonBJ 1 Reply Last reply
                                0
                                • A aftalib

                                  so i've spent most of the last night searching for a solution to this but still havent found. does anyone have an idea about this ?

                                  thank you in advance

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by JonB
                                  #27

                                  @aftalib
                                  One way, briefly, untested:

                                  connect(button1, &QPushButton::clicked, [=, button1]() { pushbutton_clicked(button1); }
                                  connect(button2, &QPushButton::clicked, [=, button2]() { pushbutton_clicked(button2); }
                                  for (auto button : each_button_when_created)
                                      connect(button, &QPushButton::clicked, [=, button]() { pushbutton_clicked(button); }
                                  
                                  void ThisClass::pushbutton_clicked(QPushButton &button)
                                  {
                                      // now button parameter is whichever button clicked
                                      // so you can use this for stuff you want to do on any of the buttons
                                      if (button.isChecked())
                                          ...
                                  }
                                  
                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    aftalib
                                    wrote on last edited by aftalib
                                    #28

                                    okay i can see this working, so i have to put the numbers of buttons i want in the line "for (auto button : each_button_when_created" ? i dont get how this line works

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      aftalib
                                      wrote on last edited by
                                      #29

                                      i just checked how the for auto loop works. would it help if i created an array of buttons in order for this to run ?

                                      JonBJ 1 Reply Last reply
                                      0
                                      • A aftalib

                                        i just checked how the for auto loop works. would it help if i created an array of buttons in order for this to run ?

                                        JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by JonB
                                        #30

                                        @aftalib
                                        Yes, but you are taking the code too literally. It doesn't matter whether you use a for on an array of buttons or whatever. In your original code I believe you have some loop which creates QPushButtons dynamically --- I assume/was assuming you create one per each some column in your QTableView? Anyway that loop is there to show you that you can connect() any time you have a dynamic pushbutton to link up, wherever it coms from, or individual button variables button1/2 if that's what you have.

                                        A 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @aftalib
                                          Yes, but you are taking the code too literally. It doesn't matter whether you use a for on an array of buttons or whatever. In your original code I believe you have some loop which creates QPushButtons dynamically --- I assume/was assuming you create one per each some column in your QTableView? Anyway that loop is there to show you that you can connect() any time you have a dynamic pushbutton to link up, wherever it coms from, or individual button variables button1/2 if that's what you have.

                                          A Offline
                                          A Offline
                                          aftalib
                                          wrote on last edited by aftalib
                                          #31

                                          @JonB okay i see what you mean, so here's what i did my code, but i have some problems :

                                           for (int i=0; i<nbOfAnt/2; i++) {
                                                       QPushButton *button = new QPushButton("H1");
                                                       index = m_table_space_pos->index(1,i,QModelIndex());
                                                       ui->tablespacepos->setIndexWidget(index, button);
                                                       button->setCheckable(true);
                                                           connect(button, &QPushButton::clicked, [=, button]() { on_H11_clicked(button); }
                                          
                                              );}
                                          

                                          the code cant run because the button argument in [=, button] must be captured with & and the argument in my slot doesnt work either. i have to use *button. but if i do so my program crashes..

                                          note : nbOfAnt is a dynamic int that changes with the will of the user and will also determine the number of buttons in the qtableview

                                          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