How to prevent the mouse click event from happening when the button is Disabled
-
Hi @ManiRon
Try this with your
setStyleSheet()
ui->pushButton->setStyleSheet("QPushButton {" "background-color:#44c767;" "}" "QPushButton:disabled {" "background-color:gray;" "}");
All the best. -
@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); }
@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 -
@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!
-
@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!
-
@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!
@J.Hilk
i am bit confused with the statement in fact."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"
- i click the button when it is disabled -> Nothing will work as Button is Disabled.
- after enabling the button the click signal gets emitted -> Yes as after 3s timer the button is enabled and can be clicked.
-
@J.Hilk
i am bit confused with the statement in fact."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"
- i click the button when it is disabled -> Nothing will work as Button is Disabled.
- after enabling the button the click signal gets emitted -> Yes as after 3s timer the button is enabled and can be clicked.
What I get out of @ManiRon statement is:
- Button is disabled
- Button gets pressed/clicked
-
- nothing happens, as expected
- Button gets set to enabled
-
- clicked signal is emitted without button interaction
Thats not normal behavior in my opinion
I'll make a quick test case
@ManiRon what Qt Version what OS btw?
-
Hmmm, Got it @J-Hilk
Thanks.@ManiRon Can you share a sample code ?
You said you are using theQTimer
.
Just wanted to check if there is anything mismatch with the code. -
@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!
@J.Hilk said in How to prevent the mouse click event from happening when the button is Disabled:
If true, then that's a bug in the library!
Nope, not true 5.12.4 MacOS, no unexpected clicked emits
int main(int argc, char *argv[]) { QApplication a(argc, argv); auto *btn = new QPushButton("Enabled"); btn->resize(200, 50); btn->show(); QObject::connect(btn, &QPushButton::clicked, [btn]()->void{ btn->setEnabled(false); btn->setText("Disabled"); qDebug("Btn clicked"); }); QObject::connect(btn, &QPushButton::pressed, []()->void{qDebug("Btn Pressed"); }); QObject::connect(btn, &QPushButton::released, []()->void{qDebug("Btn Released"); }); QTimer t_reset; t_reset.setInterval(3000); QObject::connect(&t_reset, &QTimer::timeout, btn, [btn]()->void{btn->setEnabled(true); btn->setText("Enabled");}); QObject::connect(btn, &QPushButton::clicked, &t_reset, QOverload<>::of(&QTimer::start)); return a.exec(); }
-
@J-Hilk
Even i tried on 5.12.3 - Ubuntu 16.04 & the above code ( From my post ).
Also the one shared by you.
It works fine.- No Click called related to button enable/disable status.
-
@ManiRon Please add
qDebug() << ui->pushButton->enabled();
to your slot and see what it outputs when you click on the button while it is disabled.
And please debug you app more carefully: you say you have a timeout to activate the button again (you should have said that in your first post already)? Did you make sure the button was not yet activated? Is it the same if you disable you style sheet? -
What I get out of @ManiRon statement is:
- Button is disabled
- Button gets pressed/clicked
-
- nothing happens, as expected
- Button gets set to enabled
-
- clicked signal is emitted without button interaction
Thats not normal behavior in my opinion
I'll make a quick test case
@ManiRon what Qt Version what OS btw?
-
@ManiRon Please add
qDebug() << ui->pushButton->enabled();
to your slot and see what it outputs when you click on the button while it is disabled.
And please debug you app more carefully: you say you have a timeout to activate the button again (you should have said that in your first post already)? Did you make sure the button was not yet activated? Is it the same if you disable you style sheet? -
@jsulm Yes i made sure cause i enable the button after 3s only , and this statement is not there , it throws error (qDebug() << ui->pushButton->enabled())
@ManiRon
Did you add#include <QDebug>
Ad if so -> What is the error ?
-
@ManiRon
Did you add#include <QDebug>
Ad if so -> What is the error ?
@Pradeep-P-N yes
-
@ManiRon
Did you add#include <QDebug>
Ad if so -> What is the error ?
class QPushButton' has no member named 'enabled'
qDebug() << ui->pushbutton->enabled();
^ -
class QPushButton' has no member named 'enabled'
qDebug() << ui->pushbutton->enabled();
^ -
After the button gets enabled the signal automatically called based on my previous click
and the print came as
Inside Pushbutton
ui->Pushbutton->isEnabled() 1
Inside Pushbutton
ui->Pushbutton->isEnabled() 1
Inside Pushbutton
ui->Pushbutton->isEnabled() 1 -
After the button gets enabled the signal automatically called based on my previous click
and the print came as
Inside Pushbutton
ui->Pushbutton->isEnabled() 1
Inside Pushbutton
ui->Pushbutton->isEnabled() 1
Inside Pushbutton
ui->Pushbutton->isEnabled() 1@ManiRon You dint debug the code after you disabled right ?
- From your log i can clearly see that your
PushButton
was never disabled.
ui->pushButton->setEnabled(false); qDebug() << ui->pushButton->isEnabled();
- From your log i can clearly see that your
-
@ManiRon You dint debug the code after you disabled right ?
- From your log i can clearly see that your
PushButton
was never disabled.
ui->pushButton->setEnabled(false); qDebug() << ui->pushButton->isEnabled();
Print was done before disabling
Now done after disable
Inside Pushbutton
ui->Pushbutton->isEnabled() 0
Inside Pushbutton
ui->Pushbutton->isEnabled() 0 - From your log i can clearly see that your
-
Print was done before disabling
Now done after disable
Inside Pushbutton
ui->Pushbutton->isEnabled() 0
Inside Pushbutton
ui->Pushbutton->isEnabled() 0Hi @ManiRon
- Please don't hurry, and just don't copy paste the code from the posts.
- Adding proper
debug()
messages will help us to understand the code flow and resolve the issue. @jsulm @J-Hilk (Correct me if wrong) - Here the thing is to debug the flow and code.
- We should check the flow of the code with respect to issue.
- Button is disabled.
- Button clicked.
- Button gets set to enabled after few seconds.
- clicked signal is emitted without button interaction.
All the best.