Search code examples
c++qtqpushbuttonqcheckbox

How to disable a QPushButton when deselect a QCheckBox


I'm working in a Qt project where the user can enable or disable a QPushButton by selecting or deselecting a QCheckBox.
I have already developed a function where the mouse event enable the QPushButton, but the problem is that when i deselect the QCheckBox the QPushButton remain enabled.
Is there a Qt function to disable a button when QCheckBox are unchecked?

This is the code i wrote:

//Class.h
//Function to enable the button
private slots:
   void on_QCheckBox_clicked();
//Class.cpp
void class::on_QCheckBox_clicked() {
  ui->QPushButton->setEnabled(true);
  //Here i enabled the button with setEnabled function
}

Solution

  • Thanks to @Scheff!

    I solved by using QObject::connect() to connect my QCheckBox and my QPushButton together

    Here is an example:

    class::class() {
      QObject::connect(ui->QCheckBox, &QCheckBox::toggled, ui->QPushButton, &QPushButton::setEnabled);
      //Where ui->QCheckBox is the checkbox and ui->QPushButton is your button
    }