PDA

View Full Version : disable myself when clicked



nifei
19th February 2009, 04:11
connect( button, SIGNAL(clicked()), this, SLOT( on_button_clicked()));

This::on_button_clicked()
{
button->setEnabled(false);
}


this does not work, of course. how to implement to disable a button when it is clicked?

aamer4yu
19th February 2009, 04:23
does ur cod reach the slot ?
Are you using your own slot ? or is it the default slot : on_XXXX_clicked() ?

BTW, how do you plan to enable it again ? bec you wont be able to click a disabled button ;)

nifei
19th February 2009, 05:10
does ur cod reach the slot ?
Are you using your own slot ? or is it the default slot : on_XXXX_clicked() ?

BTW, how do you plan to enable it again ? bec you wont be able to click a disabled button ;)

first question, yes, i could hit the break point in this slot, and the enabled state remains false until exiting this field.

second question, i could enable it by other object's signal

Thanks for ur reply.

spirit
19th February 2009, 06:41
try this


connect(button, SIGNAL(clicked(bool)), button, SLOT(setEnabled(bool)));

jogeshwarakundi
19th February 2009, 07:38
does ur cod reach the slot ?
Are you using your own slot ? or is it the default slot : on_XXXX_clicked() ?

BTW, how do you plan to enable it again ? bec you wont be able to click a disabled button ;)

the setEnabled() kind of functions do not reflect the change till the slot is complete and control goes back to event loop. and as explained below by others, if the only thing you have to do is to disable/enable the button, best is to connect it to the button's slot

cheers!

nifei
19th February 2009, 09:44
i created a test project like this:


connect(ui.btn, SIGNAL(clicked()), this, SLOT(on_btn_clicked()));
connect(ui.btn_2, SIGNAL(clicked()), this, SLOT(on_btn_2_clicked()));
//...
THIS::on_btn_clicked()
{
ui.btn->setEnabled(false);
ui.btn_2->setEnabled(true);
}
THIS::on_btn_2_clicked()
{
ui.btn_2->setEnabled(false);
ui.btn->setEnabled(true);
}


and it works well. my connections are more complicated: like this:


//calendar is a QCalendarWidget, currentPageChanged(int, int ) is it's signal, this signal is //often emitted the same time the QPushButton prevMonth and nextMonth is clicked
//currentPageChagned(int,int ) is not triggered by the buttons' clicking
//on_currentPage_changed(int, int) is my slot
void MyWidget::on_currentPage_changed(int, int)
{
prevMonth->setEnabled(false);
nextMonth->setEnabled(true);
}

Lykurg
19th February 2009, 10:01
Looks, like you could have a look at QWizard...

Lykurg