how to using the button setenabled when clicked itself?
-
so anybody could help me ?
or just give me a little tips?does anyone know it?please
-
@nicker-player said in how to using the button setenabled when clicked itself?:
so anybody could help me ?
You posted 7 hours ago ... please wait a little bit longer - noone is sitting here and waits to answer your questions.
-
@nicker-player
I cannot see anything wrong with your code, it looks fine. I do not recognise/know of behaviour where "dosent refreshed at the same time,always has a deleyed time".Can you reproduce this behaviour in a minimal, standalone, complete program, using the code you show? We need to rule out any other code you might have. If so please paste that, and state which version of Qt you are using on what platform.
-
please just look for the button named 2 ,just check it very carefully.
it has a shadow flash after the other button was clicked.The buttons were not disenabled the same time ,always has a delayed time.
i dont know why,ist there any reson ?or just iam using the code by the wrong way?
here is the code below.#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class QPushButton; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void slotChangePage(); signals: void signalChangePage(int); private: Ui::MainWindow *ui; bool clicktmp; QPushButton *mBtnNext;//UiBaseButton QPushButton *mBtnPrev; QPushButton *mBtnFirst; QPushButton *mBtnLast; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPushButton> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) ,clicktmp(false) { ui->setupUi(this); mBtnNext = new QPushButton(this); mBtnNext->setFixedSize(30,20); mBtnNext->setText("1"); mBtnNext->setGeometry(0,20,32,20); mBtnNext->setObjectName("1"); mBtnLast = new QPushButton(this); mBtnLast->setFixedSize(30,20); mBtnLast->setText("2"); mBtnLast->setGeometry(0,60,32,20); mBtnLast->setObjectName("2"); mBtnPrev = new QPushButton(this); mBtnPrev->setFixedSize(30,20); mBtnPrev->setText("3"); mBtnPrev->setGeometry(0,100,32,20); mBtnPrev->setObjectName("3"); mBtnFirst = new QPushButton(this); mBtnFirst->setFixedSize(30,20); mBtnFirst->setText("4"); mBtnFirst->setGeometry(0,140,32,20); mBtnFirst->setObjectName("4"); connect(mBtnNext,SIGNAL(clicked()),this,SLOT(slotChangePage())); connect(mBtnPrev,SIGNAL(clicked()),this,SLOT(slotChangePage())); connect(mBtnFirst,SIGNAL(clicked()),this,SLOT(slotChangePage())); connect(mBtnLast,SIGNAL(clicked()),this,SLOT(slotChangePage())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotChangePage(){ QPushButton *btn = qobject_cast<QPushButton *>(sender()); if(btn->objectName()=="1"){ clicktmp=!clicktmp; mBtnNext->setEnabled(false); mBtnLast->setEnabled(false); mBtnPrev->setEnabled(true); mBtnFirst->setEnabled(true); } else if(btn->objectName()=="2"){ clicktmp=!clicktmp; mBtnNext->setEnabled(false); mBtnLast->setEnabled(false); mBtnPrev->setEnabled(true); mBtnFirst->setEnabled(true); } else if(btn->objectName()=="3"){ clicktmp=!clicktmp; mBtnNext->setEnabled(true); mBtnLast->setEnabled(true); mBtnPrev->setEnabled(false); mBtnFirst->setEnabled(false); } else if(btn->objectName()=="4"){ clicktmp=!clicktmp; mBtnNext->setEnabled(true); mBtnLast->setEnabled(true); mBtnPrev->setEnabled(false); mBtnFirst->setEnabled(false); } }
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
-
- Is this indeed your whole code? So you define a signal
signalChangePage(int)
but never emit it? - Put a
qDebug()
statement insideslotChangePage()
so you can see when/how often it is called. - You might as well reduce it having just 2 buttons which show the issue?
- Is this indeed your whole code? So you define a signal
-
@nicker-player said in how to using the button setenabled when clicked itself?:
, ui(new Ui::MainWindow)
Also when we should be able to reproduce it remove the ui and put all in the code.
-
-
signalChangePage(int) was a function i've forgot to use ,it dosent matter.It was just a emtpy signalfunction
-
I've put the qDebug() in the slotChangePage() method,but it just called normally,i dident see any unusaul output from the qdebug information.
-
the problem is very obviously from the flash what I've uploaded above,the two buttons dosent became shadowed in the same time,one of them has delayed from the others
-
, ui(new Ui::MainWindow) ,the code seems doesnt matter.Because I've removed the ui code in the other project.
-
-
Possible error caused by objectname()。
Is the same objectname set for these buttons in the ui file?
Maybe you can split slotChangePage into 4 slot functions which not use objectName. and test again.
I tend to use QStateMachine for this kind of ui state switching -
Your example code works fine for me once the ui stuff is fixed.
-
@whut
If I put the diffrent slot methods,the errors dosent disappeared.
I think its not the main reason .And how to use the QStateMachine for this kind of ui state?
Would you give me some little tips?or a href ?
This is an annoysing problem.@Christian-Ehrlicher
And how to fix ui stuff you just mentioned?I dont know what are you talking about.
Could you give me a little example?Thanks a lot. -
This doesn't look like the problem of your code but rather the refresh rate. It doesn't update so slow for me.
Each separatesetEnabled()
call triggers an update and usually they would be lumped together and redrawn at the same time, but if, for some reason, your refresh is extremely low these two updates could get separated into two refreshes giving you the effect you see.Seeing the window style it looks like you're running this under Windows with advanced compositor disabled. That could be the culprit. Is that maybe under a virtual machine or through a remote desktop?
Not sure if it would work but you can try to force the update to happen at the same time like this:
setUpdatesEnabled(false); //disable updates mBtnNext->setEnabled(false); mBtnLast->setEnabled(false); mBtnPrev->setEnabled(true); mBtnFirst->setEnabled(true); setUpdatesEnabled(true); // re-enable updates update(); // manually force update
-
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QState> #include <QStateMachine> #include <QFinalState> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); signals: //this signals may be emit by buttons click, keys events,wheel events, slider move. void sigPageGoStart(); void sigPageGoMid(); void sigPageGoEnd(); void sigPageClose();//this signal emit when close current page,machine will finsh private slots: //Here I only add the button trigger emit state Transition procedure void on_pushButton1_First_clicked(); void on_pushButton_Prev_clicked(); void on_pushButton_Next_clicked(); void on_pushButton_Last_clicked(); void slotPageFinishedState(); private: Ui::MainWindow *ui; QStateMachine* pageStateMachine=new QStateMachine(this); QState* pageStartState=nullptr; QState* pageMiddleState=nullptr; QState* pageEndState=nullptr; QFinalState* pageNoneState=nullptr; void OpenPage(); void ClosePage(); }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //1-create state pageStartState=new QState(pageStateMachine); pageStartState->assignProperty(ui->pushButton1_First,"enabled",false); pageStartState->assignProperty(ui->pushButton_Prev,"enabled",false); pageStartState->assignProperty(ui->pushButton_Next,"enabled",true); pageStartState->assignProperty(ui->pushButton_Last,"enabled",true); pageMiddleState=new QState(pageStateMachine); pageMiddleState->assignProperty(ui->pushButton1_First,"enabled",true); pageMiddleState->assignProperty(ui->pushButton_Prev,"enabled",true); pageMiddleState->assignProperty(ui->pushButton_Next,"enabled",true); pageMiddleState->assignProperty(ui->pushButton_Last,"enabled",true); pageEndState=new QState(pageStateMachine); pageEndState->assignProperty(ui->pushButton1_First,"enabled",true); pageEndState->assignProperty(ui->pushButton_Prev,"enabled",true); pageEndState->assignProperty(ui->pushButton_Next,"enabled",false); pageEndState->assignProperty(ui->pushButton_Last,"enabled",false); pageNoneState=new QFinalState(pageStateMachine); connect(pageStateMachine,&QStateMachine::stopped,this,&MainWindow::slotPageFinishedState); //2-add state transition pageStartState->addTransition(this,&MainWindow::sigPageGoMid,pageMiddleState); pageStartState->addTransition(this,&MainWindow::sigPageGoEnd,pageEndState); pageStartState->addTransition(this,&MainWindow::sigPageClose,pageNoneState); pageMiddleState->addTransition(this,&MainWindow::sigPageGoStart,pageStartState); pageMiddleState->addTransition(this,&MainWindow::sigPageGoEnd,pageEndState); pageMiddleState->addTransition(this,&MainWindow::sigPageClose,pageNoneState); pageEndState->addTransition(this,&MainWindow::sigPageGoMid,pageMiddleState); pageEndState->addTransition(this,&MainWindow::sigPageGoStart,pageStartState); pageEndState->addTransition(this,&MainWindow::sigPageClose,pageNoneState); //3- open Page OpenPage(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton1_First_clicked() { //page move to start page view emit sigPageGoStart(); } void MainWindow::on_pushButton_Prev_clicked() { //page move up //Check page view position, send signal sigPageGoStart\sigPageGoMid\sigPageGoEnd } void MainWindow::on_pushButton_Next_clicked() { //page move down //Check page view position, send signal sigPageGoStart\sigPageGoMid\sigPageGoEnd } void MainWindow::on_pushButton_Last_clicked() { //page move to start page view emit sigPageGoEnd(); } void MainWindow::slotPageFinishedState() { ui->pushButton1_First->setEnabled(false); ui->pushButton_Prev->setEnabled(false); ui->pushButton_Next->setEnabled(false); ui->pushButton_Last->setEnabled(false); } void MainWindow::OpenPage() { //1-open page move to start page view //2-set ui machine pageStateMachine->setInitialState(pageStartState); pageStateMachine->start(); } void MainWindow::ClosePage() { //1-close page //2-stop ui machine emit sigPageClose(); }