QT disable Qpushbutton

Asked

Viewed 38 times

1

I created a form with several Qpushbuttons and turned on a SLOT to each of the buttons. At this moment I have, for each button, a SLOT different. Even if each button performs a different function, part of the processor is equal regardless of the button. At the end the pressed button is disabled.

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

1 answer

1


Yes, it’s possible. And it’s easier than it might seem in the beginning. You can use the function 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
    }
}

A final note, it is not possible to use this method when the SLOT is called through Qt::Directconnection from a different thread. Do not use this function in that particular case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.