QT disable QPushButton

1

I created a form with several QPushButtons and linked a SLOT to each of the buttons. At this moment I have, for each button, a SLOT different. Although each button performs a different function, part of the process is equal regardless of the button. At the end the pressed button is disabled.

My question is this: Is it possible to create a generic SLOT that would bind to each of the buttons, and within that, determine which button was pressed?

    
asked by anonymous 15.07.2015 / 22:01

1 answer

1

Yes, it is possible. And it's easier than it might seem at first. You can use the sender ()

void processEvent() {

    QPushButton *button_ = qobject_cast<QPushButton *>(sender());

    if (button_) {
       if (button_ == button1) {     //button1 foi pressionado

           executeSomethingB1();
       } 
       else {
          if (button_ == button2) {  //button1 foi pressionado
             executeSomethingB2();
          }
       }

       executeGenericAction();       //ação genérica
       button_->setEnabled(false);   //desactiva o botão que foi pressionado
    }
}

One final note, you can not use this method when SLOT is called through Qt :: DirectConnection from a different thread. Do not use this function in this particular case.

    
15.07.2015 / 22:03