gpt4 book ai didi

c++ - Qt 状态机 : How to associate a single transition with multiple states

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:01:16 24 4
gpt4 key购买 nike

问题

我目前正在开发一个使用 Qt 状态机框架的应用程序,我的目标是将单个转换与多个源状态相关联。这样做的理由是导致转换的信号独立于源状态,因此我认为最好有一个而不是多个转换(每个源状态一个)。我在通过 Qt 实现上述目标时遇到了问题。下面使用测试状态机演示了详细信息。 (下面提到的是一个假设的状态机,但我也可以为这种用例提供​​一些真实世界的例子)。请提出一些有效的方法来实现上述目标。

问题所在的代码片段

m_s1.addTransition(&m_Trans); // Adding transition to state S1
m_s2.addTransition(&m_Trans); // Adding the same transition to state S2
// As per Qt docs, it seems the ownership of thr transition will be transferred to s2 which is what is causing the problem.

enter image description here

CState.hpp

class CState: public QState
{

public:

/** Constructor */
CState(std::string a_pStateName)
:QState(nullptr),
m_pStateName(a_pStateName)
{

}

/** Overriding on entry */
virtual void onEntry(QEvent *a_pEvent) Q_DECL_OVERRIDE
{
(void) a_pEvent;
printf("State entry %s\n",m_pStateName.c_str());
}

~CState() {}
CState() {}
private:
std::string m_pStateName;
};

CTestMachine.hpp

class CTestStateMachine: public QObject
{
Q_OBJECT

public:
CTestStateMachine();
~CTestStateMachine() {};

private:
QSignalTransition m_Trans;
CState m_s1;
CState m_s2;
CState m_s3;
QStateMachine m_TestMachine;
QTimer m_Timer;

signals:
void SourceIndependentSignal();

public slots:
void TimetoInvokeTrans();


};

CTestMachine.cpp

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

#include <QObject>
#include <QCoreApplication>
#include <QStateMachine>
#include <QState>
#include <QSignalTransition>
#include <QTimer>

#include "CState.hpp"
#include "CTestStateMachine.hpp"

void CTestStateMachine::TimetoInvokeTrans()
{
printf("Emitting source independent signal\n");
emit SourceIndependentSignal();
}

CTestStateMachine::CTestStateMachine():
m_Trans(this, SIGNAL(SourceIndependentSignal())),
m_s1("s1"),
m_s2("s2"),
m_s3("s3")
{
/* Setup state machine */
m_Trans.setTargetState(&m_s3);
m_s1.addTransition(&m_Trans);
m_s2.addTransition(&m_Trans);
m_TestMachine.addState(&m_s1);
m_TestMachine.addState(&m_s2);
m_TestMachine.addState(&m_s3);
m_TestMachine.setInitialState(&m_s1);
m_TestMachine.start();
printf("Started state machine\n");

/* Trigger timer to make transitions */
connect(&m_Timer, SIGNAL(timeout()), this, SLOT(TimetoInvokeTrans()));
m_Timer.start(1000);
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
CTestStateMachine TestMachine;
return a.exec();
}

最佳答案

您可以在进入状态时移动转换。

connect(m_state, &QState::entered, [m_state, m_tr]() -> void { m_state->addTransition(m_tr); });

或者只是让父状态保持转换并将转换类型设置为内部。

QState *s = new QState(m_stateMachine);
QState *s1 = new QState(s);
QState *s2 = new QState(s);
QState *s3 = new QState(s);
QSignalTransition *sTr = new QSignalTransition(sender, SIGNAL(foobar), s);
sTr->setTargetState(s3);
sTr->setTransitionType(QAbstractTransition::InternalTransition);

关于c++ - Qt 状态机 : How to associate a single transition with multiple states,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com