Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. how to using the button setenabled when clicked itself?
Forum Updated to NodeBB v4.3 + New Features

how to using the button setenabled when clicked itself?

Scheduled Pinned Locked Moved Unsolved Qt 6
13 Posts 5 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nicker playerN Offline
    nicker playerN Offline
    nicker player
    wrote on last edited by
    #1
    int  onInitSlots()
    { 
        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()));
    }
    
    
    void slotChangePage(){
        QPushButton *btn = qobject_cast<QPushButton *>(sender());
        QString text = btn->text();
        QString name = sender()->objectName();
        int pageCount = mPageCtl->getPagescount();
        int pageCur = mPageCtl->getPageid();
        bool bprev = !(pageCur==0);
        bool bnext = !(pageCount==pageCur+1);
        mBtnPrev->setEnabled(bprev);
        mBtnFirst->setEnabled(bprev);
        mBtnNext->setEnabled(bnext);
        mBtnLast->setEnabled(bnext);
    }
    

    when using the code above ,the button for example,like the mBtnPrev and mBtnFirst button dosent refreshed at the same time,always has a deleyed time when one button clicked and refreshed ,then the other button refreshed (setEnabled function)

    I'm debugging the code,and cant find the reason why the two of the buttons dosent refreshed the same time,somebody told me that maybe caused by the button slot function, I've tried the update() or qApp->processevents() function anyway,and it seems dosent work.

    So could you help me ?please its an annoying problem.

    JonBJ 1 Reply Last reply
    0
    • nicker playerN Offline
      nicker playerN Offline
      nicker player
      wrote on last edited by
      #2

      so anybody could help me ?
      or just give me a little tips?

      does anyone know it?please

      Christian EhrlicherC 1 Reply Last reply
      0
      • nicker playerN nicker player

        so anybody could help me ?
        or just give me a little tips?

        does anyone know it?please

        Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • nicker playerN nicker player
          int  onInitSlots()
          { 
              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()));
          }
          
          
          void slotChangePage(){
              QPushButton *btn = qobject_cast<QPushButton *>(sender());
              QString text = btn->text();
              QString name = sender()->objectName();
              int pageCount = mPageCtl->getPagescount();
              int pageCur = mPageCtl->getPageid();
              bool bprev = !(pageCur==0);
              bool bnext = !(pageCount==pageCur+1);
              mBtnPrev->setEnabled(bprev);
              mBtnFirst->setEnabled(bprev);
              mBtnNext->setEnabled(bnext);
              mBtnLast->setEnabled(bnext);
          }
          

          when using the code above ,the button for example,like the mBtnPrev and mBtnFirst button dosent refreshed at the same time,always has a deleyed time when one button clicked and refreshed ,then the other button refreshed (setEnabled function)

          I'm debugging the code,and cant find the reason why the two of the buttons dosent refreshed the same time,somebody told me that maybe caused by the button slot function, I've tried the update() or qApp->processevents() function anyway,and it seems dosent work.

          So could you help me ?please its an annoying problem.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • nicker playerN Offline
            nicker playerN Offline
            nicker player
            wrote on last edited by
            #5

            mnggiflab-download.gif
            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();
            }
            
            
            JonBJ Christian EhrlicherC 2 Replies Last reply
            0
            • nicker playerN nicker player

              mnggiflab-download.gif
              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();
              }
              
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @nicker-player

              • Is this indeed your whole code? So you define a signal signalChangePage(int) but never emit it?
              • Put a qDebug() statement inside slotChangePage() so you can see when/how often it is called.
              • You might as well reduce it having just 2 buttons which show the issue?
              nicker playerN 1 Reply Last reply
              0
              • nicker playerN nicker player

                mnggiflab-download.gif
                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();
                }
                
                
                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • JonBJ JonB

                  @nicker-player

                  • Is this indeed your whole code? So you define a signal signalChangePage(int) but never emit it?
                  • Put a qDebug() statement inside slotChangePage() so you can see when/how often it is called.
                  • You might as well reduce it having just 2 buttons which show the issue?
                  nicker playerN Offline
                  nicker playerN Offline
                  nicker player
                  wrote on last edited by
                  #8

                  @JonB

                  • 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.

                  W 1 Reply Last reply
                  0
                  • nicker playerN nicker player

                    @JonB

                    • 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.

                    W Offline
                    W Offline
                    whut
                    wrote on last edited by
                    #9

                    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

                    nicker playerN 1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Your example code works fine for me once the ui stuff is fixed.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • W whut

                        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

                        nicker playerN Offline
                        nicker playerN Offline
                        nicker player
                        wrote on last edited by
                        #11

                        @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.

                        W 1 Reply Last reply
                        0
                        • Chris KawaC Online
                          Chris KawaC Online
                          Chris Kawa
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          This doesn't look like the problem of your code but rather the refresh rate. It doesn't update so slow for me.
                          Each separate setEnabled() 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
                          
                          1 Reply Last reply
                          2
                          • nicker playerN nicker player

                            @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.

                            W Offline
                            W Offline
                            whut
                            wrote on last edited by
                            #13

                            @nicker-player

                            #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();
                            }
                            
                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved