Solved Change state through c ++
-
I am developing an application using state machine but it is showing error !!
follow the code to take a look !!anyone who can help me will be grateful!
#ifndef CHANGEDSTATE_H
#define CHANGEDSTATE_H#include <QObject>
#include <QTimer>
#include <QDebug>class ChangedState: public QObject
{Q_OBJECT
public:
ChangedState(QObject *parent = nullptr);~ChangedState(); Q_INVOKABLE void goTela(void); int ikeresp() const;
signals:
void changeScren(void);public slots: void setScreen(void);
private:
int resp; ChangedState *t2;
};
#endif // CHANGEDSTATE_H
#include "changedstate.h"
#include <QMetaObject>
#include <QObject>
ChangedState::ChangedState(QObject *parent) : QObject(parent)
{
ChangedState *t2 = new ChangedState;connect(t2,&ChangedState::setScreen,this,&ChangedState::changeScren);
}
ChangedState::~ChangedState()
{
delete t2;
}void ChangedState::goTela()
{
this->setScreen();
}int ChangedState::ikeresp() const
{
return resp;
}void ChangedState::setScreen(void)
{
emit changeScren();
}main.cpp
qmlRegisterType<ChangedState>("Astus", 1,0, "Ikegami");
engine.rootContext()->setContextProperty("changedDevice",&device);
.qml
Connections
{
target:changedDevice
onChangeScren:
{root.state = "tela1" } } }
-
Hi
You forgot to post the actual error :) -
-
The error is: the program has unexpectedly finished!
-
-
@mrjj
unfortunately the mistake continues !!
It is as if the program opens but it is finished! -
@Rosimar use a debugger to step through your program to find the problematic code.
-
What is the meaning of you connect statement ? Is it signal/slot or signal/signal ? I did not find correct combination at all.also show the code which u changed to handle dangling pointer as suggested by @aha_1980
-
@Rosimar said in Change state through c ++:
ChangedState *t2 = new ChangedState;
Your issue is
ChangedState *t2 = new ChangedState;You are you creating the same object inside the constructor of same object ? This calls constructor repeatedly. Just put a debug statement inside the constructor. You will see the fun.
-
estou enviando um link do projeto pra ver o que há de errado
-
As I said your program is crashing because in the constructor you are creating the same object. It get into infinite loop & finally crashes.
Can you tell me the are you creating the same object *t2 = new ChangedState; ? This is NOT required. Remove this & rest should work fine.You have register ChangeState to QML as type. Also you create the object & share it. Any idea why are you doing both
Finally what you wanted to achieve ?
-
the idea would be when touching the screen to issue a command to the c ++ side and send a signal to change that in qml!
-
Remove the objects creation in constructor and everything shud work fine. It will do what u wanted to achieve.
-
Hi Dheerendra, I did what you suggested, but the result was not what I expected! I do not know what's wrong!
-
When you clicked on the UI, did it call back-end c++ function ? Please confirm. Based on this I will tell you next steps.
-
yes when clicking the screen it calls a function in c ++ that emits a signal !!
-
In your Tela.qml replace the following line#58. Then tell me what happens.
Connections { target:ike onChangeScren: { console.log("gfrde") root.state = "tela1" } }
-
Now it worked thank God !!
Thank you so much again!!
Thank you very much!!