How to prevent the mouse click event from happening when the button is Disabled
-
@ManiRon Just did the same: disabled button after first click. Works as expected for me: after disabling clicked signal is not emitted. Is the button shown as greyed out after disabling? Are you sure you're disabling the correct button?
By the way: it is setDisabled not setdisabled. -
@ManiRon Are you sure you're disabling the correct button?
As I said: I can't reproduce such behaviour, there must be something in your code.
Also you can disable your style sheet and test again.
And to be sure: in the title you're talking about event, but you mean clicked() signal, correct? -
-
I think the problem here for @ManiRon is the style sheet for the
QPushButton
The Button is actually disabled on Click, but due to the StyleSheet the color difference is not found.
void MainWindow::on_pushButton_clicked() { qDebug() << Q_FUNC_INFO; ui->label->setText("Button is Disabled"); ui->pushButton->setEnabled(false); }
-
@ManiRon
I just tried with Timer it works fine.
The Button is disabled for 3s.- Check with your stylesheet.
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->label->setVisible(false); tmr = new QTimer(this); connect(tmr, &QTimer::timeout, [this]{ ui->pushButton->setEnabled(true); }); } void MainWindow::on_pushButton_clicked() { qDebug() << Q_FUNC_INFO << QTime::currentTime().toString(); tmr->start(); tmr->setInterval(3000); ui->pushButton->setEnabled(false); }
-
Hi @ManiRon
Try this with your
setStyleSheet()
ui->pushButton->setStyleSheet("QPushButton {" "background-color:#44c767;" "}" "QPushButton:disabled {" "background-color:gray;" "}");
All the best. -
@Pradeep-P-N @jsulm @J-Hilk
when i created a sample i was absorbing one thing , that when i click the button i disabled it , but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed by the click signal which was occuring when i clicked the button when it was disabled -
@ManiRon said in How to prevent the mouse click event from happening when the button is Disabled:
but while i click the button when it is disabled after enabling the button the click signal gets emitted and the click function is executed
If true, then that's a bug in the library!