Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to override QState and use it

how to override QState and use it

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 2 Posters 5.4k 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.
  • A Offline
    A Offline
    AndreAhmed
    wrote on last edited by
    #1

    I'm trying to override QState functions onEntry and onExist by its child class . How would I call the constructor of the QState. I'm so confused about reimplementing the QState .

    
    class s : public QState
    {
    public:
        s(QState *parent = 0);
        ~s();
    protected:
        void onEntry(QEvent *event) override;
        void onExit(QEvent *event);
    
    };
    void s::onEntry(QEvent *event)
    {
        qDebug("on enter");
    }
    
    void s::onExit(QEvent *event)
    {
    
    }
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        s * state = new s();
    }
    and QState is like:
    
    class QState
    {
    protected:
         .
         .
    public:
         void onEntry(QEvent *event);
         void onEntry(QEvent *event)
    

    };

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      you call your base class constructor like this
      s::s(QWidget* parent) : QState(parent) {}

      1 Reply Last reply
      1
      • A Offline
        A Offline
        AndreAhmed
        wrote on last edited by
        #3

        so I have to have a dummy initial state and then pass it to the abouve s::(QWidget*parent) ?

        mrjjM 1 Reply Last reply
        0
        • A AndreAhmed

          so I have to have a dummy initial state and then pass it to the abouve s::(QWidget*parent) ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @AndreAhmed
          From your own constructor, you also call base class constructor
          as part of constructing your QState child.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AndreAhmed
            wrote on last edited by
            #5

            what should I pass a dummy QState or a QStatemachine ?

            mrjjM 1 Reply Last reply
            0
            • A AndreAhmed

              what should I pass a dummy QState or a QStatemachine ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @AndreAhmed
              I think u can pass a Dummy but not sure u need to do it?

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                hi
                this sample also inherited QState
                http://doc.qt.io/qt-5/qtwidgets-statemachine-pingpong-example.html

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  AndreAhmed
                  wrote on last edited by
                  #8

                  do you know how should I keep the state active until a condition occurs ? How to implement that ?

                  mrjjM 1 Reply Last reply
                  0
                  • A AndreAhmed

                    do you know how should I keep the state active until a condition occurs ? How to implement that ?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @AndreAhmed
                    well a given state is active when no Transition has happened.

                    do you mean like a function being called over and over while in a state?

                    1 Reply Last reply
                    1
                    • A Offline
                      A Offline
                      AndreAhmed
                      wrote on last edited by
                      #10

                      yes I mean a function that's called over and over , then after a condition like if(cnt==5) go to next state.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        AndreAhmed
                        wrote on last edited by
                        #11

                        a giving state is active but it only calls onEnter() just once.

                        mrjjM 1 Reply Last reply
                        0
                        • A AndreAhmed

                          a giving state is active but it only calls onEnter() just once.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @AndreAhmed
                          Hi
                          Look here
                          https://doc.qt.io/archives/4.6/statemachine-trafficlight.html

                          They use a timer.
                          You could do the same and simply enable onEnter
                          and turn off onExit.

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            AndreAhmed
                            wrote on last edited by
                            #13

                            hi,

                            thanks for the answer. How would I put a timer for onEntry() I'm so confused

                            mrjjM 1 Reply Last reply
                            0
                            • A AndreAhmed

                              hi,

                              thanks for the answer. How would I put a timer for onEntry() I'm so confused

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #14

                              @AndreAhmed
                              Add a QTimer timer; to your state
                              add a slot to your state
                              connect timer to slot

                              OnEnter
                              timer.start(1000); // call ur slot every 1 sec

                              OnExit
                              timer.stop();

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                AndreAhmed
                                wrote on last edited by
                                #15

                                Can I say addtransition inside the slot of the timer so that to move to another state ?

                                how to manage states then, they should be in one cpp file ?

                                mrjjM 1 Reply Last reply
                                0
                                • A AndreAhmed

                                  Can I say addtransition inside the slot of the timer so that to move to another state ?

                                  how to manage states then, they should be in one cpp file ?

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by mrjj
                                  #16

                                  @AndreAhmed
                                  You can use any normal QState function in the slot to advance state.

                                  Well, I guess keeping related states in same cpp sounds like good plan.

                                  You did see
                                  http://doc.qt.io/qt-5/statemachine-api.html
                                  ?

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    AndreAhmed
                                    wrote on last edited by AndreAhmed
                                    #17

                                    Yes I did but didn't know how to use the classes of the QState,..etc.

                                    So the steps now

                                    1. Inherit from QState and implement onEnter on Exit
                                    2. make a timer
                                    3. start the timer in onEnter()
                                    4. do the logic inside the slot of the timer
                                    5. add the transition there
                                    mrjjM 1 Reply Last reply
                                    0
                                    • A AndreAhmed

                                      Yes I did but didn't know how to use the classes of the QState,..etc.

                                      So the steps now

                                      1. Inherit from QState and implement onEnter on Exit
                                      2. make a timer
                                      3. start the timer in onEnter()
                                      4. do the logic inside the slot of the timer
                                      5. add the transition there
                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      hi
                                      Yes something like that. Please make Timer a class member.

                                      Just a note:
                                      Well it has signals also
                                      void entered()
                                      void exited()
                                      http://doc.qt.io/qt-5/qabstractstate.html#entered
                                      So you dont need to subclass to use Enter/exit.

                                      1 Reply Last reply
                                      1

                                      • Login

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