由于关于此的内容很少……这是我的 2p(对不起,英语)……
我的 DataModel.h 看起来像这样
#ifndef DATAMODEL_H
#define DATAMODEL_H
#include <QDebug>
#include <QScxmlCppDataModel>
#include <QScxmlEvent>
class DataModel :public QScxmlCppDataModel
{
Q_OBJECT
Q_SCXML_DATAMODEL
public:
DataModel(QObject *parent);
void UpdateFields(QString call, int rst, QString exchange);
bool CallIsValid(QString s);
bool RstIsValid(int i);
bool ExchangeIsValid(QString s);
QString Call;
int Rst;
QString Exchange;
QString m_Descript;
QVariant m_var;
};
#define DATAMODEL_H
#endif // DATAMODEL_H
它的实现我喜欢这样......
#include "DataModel.h"
DataModel::DataModel(QObject *parent):
QScxmlCppDataModel(parent)
{
qDebug() << "Data Model Initalized";
}
void DataModel::UpdateFields(QString call, int rst, QString exchange)
{
Call=call;
Rst=rst;
Exchange=exchange;
}
bool DataModel::CallIsValid(QString s)
{
if (s.length()>4)
{
Call=s;
return true;
}
else
return false;
}
我的测试状态引擎有 3 个状态,呼叫、RST、交换......然后调用它们之间的事件
所以我的 MainWindow 标题看起来像
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QScxmlStateMachine>
#include <QScxmlCppDataModel>
#include <QDebug>
#include "DataModel.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
// These are the States
void onCallState(bool isActive);
void onRstState(bool isActive);
void onExchangeState(bool isActive);
// These are the Events
void ongotCallEvent(const QScxmlEvent &event);
void ongotRstEvent(const QScxmlEvent &event);
void ongotExchangeEvent(const QScxmlEvent &event);
public slots:
void OnReturnPressed();
private:
Ui::MainWindow *ui;
QScxmlStateMachine *m_stateMachine;
DataModel *datamodel;
};
#endif // MAINWINDOW_H
实现是完成所有“布线”的地方......
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->f1,SIGNAL(returnPressed()),this,SLOT(OnReturnPressed()));
connect(ui->f2,SIGNAL(returnPressed()),this,SLOT(OnReturnPressed()));
connect(ui->f3,SIGNAL(returnPressed()),this,SLOT(OnReturnPressed()));
m_stateMachine = QScxmlStateMachine::fromFile(":/QsoEx.scxml");
for(QScxmlError& error:m_stateMachine->parseErrors())
{
qDebug()<<error.description();
}
// Connect up the States
// We have 3 States... Call Rst Exchange
m_stateMachine->connectToState("Call", this, &MainWindow::onCallState);
m_stateMachine->connectToState("Rst", this, &MainWindow::onRstState);
m_stateMachine->connectToState("Exchange", this, &MainWindow::onExchangeState);
// We have 3 Events which move us between each State
m_stateMachine->connectToEvent("gotCall", this, &MainWindow::ongotCallEvent);
m_stateMachine->connectToEvent("gotRst", this, &MainWindow::ongotRstEvent);
m_stateMachine->connectToEvent("gotExchange", this, &MainWindow::ongotExchangeEvent);
datamodel = new DataModel(this);
m_stateMachine->setDataModel(datamodel);
m_stateMachine->init();
m_stateMachine->start();
}
“逻辑”......即控制状态之间移动的东西 - 表达如下......字段f1,f2和f3是UI中的行输入对象......
void MainWindow::OnReturnPressed()
{
QString curState = m_stateMachine->activeStateNames()[0];
qDebug() << "On Return Pressed Triggered";
qDebug() << "Current State is " + curState;
if (curState == "Call")
{
qDebug() << "We are in Call State. Checking Call";
// We can move from call to rst is we have a valid call
if (datamodel->CallIsValid(ui->f1->text()))
{
qDebug() << "Data Is Valid";
QVariant var = QVariant(ui->f1->text());
m_stateMachine->submitEvent("gotCall", var);
ui->f2->setFocus();
}
else
{
ui->f1->setFocus();
}
}
if (curState == "RST")
{
if (datamodel->RstIsValid(QString(ui->f2->text()).toInt()))
{
QVariant var = QVariant(ui->f1->text());
m_stateMachine->submitEvent("gotRst", QVariant("Rst"));
ui->f3->setFocus();
}
else
{
ui->f2->setFocus();
}
}
if (curState == "Exchange")
{
if (datamodel->ExchangeIsValid(ui->f3->text()))
{
QVariant var = QVariant(ui->f1->text());
m_stateMachine->submitEvent("gotExchange", QVariant("Rst"));
ui->f1->setFocus();
}
else
{
ui->f3->setFocus();
}
}
ui->label->setText("State "+m_stateMachine->activeStateNames()[0]);
}
如果您想要此代码的副本,因为我可能省略了某些内容...请转到https://github.com/timseed/Ex_Qt_StateMachine