如何检测忽略/拒绝的发布到 QStateMachine 的 QEvent
我需要查看状态机的自定义发布事件是否被接受。我的方法是子类化 QStateMachine。请参阅http://doc.qt.nokia.com/latest/statemachine-api。 html 部分事件、转换和守卫
我想知道下面的代码中是否有我错过的东西。没有其他/更好的方法吗?
基本上这里与 Qt Doc 相同:
bool MyStateTransition::eventTest(QEvent *e)
{
if (e->type() != QEvent::Type(QEvent::User+1)) // MyEvent
return false;
MyEvent *se = static_cast<MyEvent*>(e);
if(m_value != se->value)
{
se->setRejected(true);
return false;
}
qDebug() << "MyStateTransition::eventTest() - Transition " << m_value << " accepting event " << se->value;
se->setRejected(false);
return true;
}
我能找到的检测拒绝的最简单方法是这样的:
void MyStateMachine::endSelectTransitions(QEvent *event)
{
if (event->type() != QEvent::Type(QEvent::User+1)) return;
MyEvent *se = static_cast<MyEvent*>(event);
if(se->rejected())
emit eventRejected(se);
else
//Not really needed since we can use triggered() which will fire after
emit eventAccepted(se);
}
I need to see if a custom posted event to the state machine was accepted or not. My approach is to subclass QStateMachine. See http://doc.qt.nokia.com/latest/statemachine-api.html section Events, Transitions and Guards
I'm wondering is there is not something I missed in the code below. Is there not another/better approach?
Basically here is the same as per the Qt Doc:
bool MyStateTransition::eventTest(QEvent *e)
{
if (e->type() != QEvent::Type(QEvent::User+1)) // MyEvent
return false;
MyEvent *se = static_cast<MyEvent*>(e);
if(m_value != se->value)
{
se->setRejected(true);
return false;
}
qDebug() << "MyStateTransition::eventTest() - Transition " << m_value << " accepting event " << se->value;
se->setRejected(false);
return true;
}
And the simplest way I could find to far to detect the reject is this:
void MyStateMachine::endSelectTransitions(QEvent *event)
{
if (event->type() != QEvent::Type(QEvent::User+1)) return;
MyEvent *se = static_cast<MyEvent*>(event);
if(se->rejected())
emit eventRejected(se);
else
//Not really needed since we can use triggered() which will fire after
emit eventAccepted(se);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,最后回答我自己的问题:它有效,但基本上是一个黑客,因为没有正式记录。
Ok, finally answering my own question: It works but is basically a hack since not officially documented.