how to override QState and use it
-
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)
};
-
hi
you call your base class constructor like this
s::s(QWidget* parent) : QState(parent) {} -
so I have to have a dummy initial state and then pass it to the abouve s::(QWidget*parent) ?
-
@AndreAhmed
From your own constructor, you also call base class constructor
as part of constructing your QState child. -
what should I pass a dummy QState or a QStatemachine ?
-
@AndreAhmed
I think u can pass a Dummy but not sure u need to do it? -
hi
this sample also inherited QState
http://doc.qt.io/qt-5/qtwidgets-statemachine-pingpong-example.html -
do you know how should I keep the state active until a condition occurs ? How to implement that ?
-
@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?
-
yes I mean a function that's called over and over , then after a condition like if(cnt==5) go to next state.
-
a giving state is active but it only calls onEnter() just once.
-
@AndreAhmed
Hi
Look here
https://doc.qt.io/archives/4.6/statemachine-trafficlight.htmlThey use a timer.
You could do the same and simply enable onEnter
and turn off onExit. -
hi,
thanks for the answer. How would I put a timer for onEntry() I'm so confused
-
@AndreAhmed
Add a QTimer timer; to your state
add a slot to your state
connect timer to slotOnEnter
timer.start(1000); // call ur slot every 1 secOnExit
timer.stop(); -
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 ?
-
@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
? -
Yes I did but didn't know how to use the classes of the QState,..etc.
So the steps now
- Inherit from QState and implement onEnter on Exit
- make a timer
- start the timer in onEnter()
- do the logic inside the slot of the timer
- add the transition there
-
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.