The first thing you have to do is capture the signal clicked()
of button1
. You can do this directly from the graphical environment or using a code similar to this:
class MainForm
{
private slots:
void button1_clicked();
};
MinForm::MainForm()
{
// ...
connect(button1,SIGNAL(clicked()),SLOT(button1_clicked()));
}
void MainForm::button1_clicked()
{
}
With this code you have already gotten every time you click in button1
you call the function button1_click
.
From this point, the resolution of the problem can be done in different ways:
1. In a traditional way
In this case you need that MainForm
have, in some way, a pointer to OtherForm
:
class MainForm
{
private:
OtherForm* otherForm;
};
Once that is achieved, the way to disable the button will depend on whether the button2
object is public or private. If it is public, deactivation can be done directly and, if not, we will have to prepare a function that allows us to modify the activation status of the button:
void MainForm::button1_clicked()
{
// Si button2 es public
otherForm->button2->setEnabled(false);
// Si es private
otherForm->setButton2Enabled(false);
}
2. With signals and slots
Another way to solve the problem is not to capture the signal in the main form but directly on the one we are going to act on.
A simple way would be to take advantage of the moment in which otherForm
is created to link the signal clicked
of button1
with this slot :
class OtherForm
{
public slots:
void CambiarEstadoBoton()
{
button2->setEnabled(!button2->isEnabled());
}
};
void MainForm::CrearOtherForm()
{
OtherForm* otherForm = new OtherForm;
connect(button1,SIGNAL(clicked()),
otherForm,SLOT(CambiarEstadoBoton()));
}
Keep in mind that QPushButton
does not have a slot that permutes the activation status. We have setEnabled
, but to make it work we have to tell you if we want to enable or disable the button ... it does not work for us unless we want that button2
the MainForm
button will be pressed. If this were the case we could save the function CambiarEstadoBoton
and link the signal of button1
with the slot of button2
:
void MainForm::CrearOtherForm()
{
OtherForm* otherForm = new OtherForm;
connect(button1,SIGNAL(toggled(bool)),
otherForm,SLOT(setEnabled(bool)));
}