如何检测忽略/拒绝的发布到 QStateMachine 的 QEvent

发布于 2024-10-14 07:32:42 字数 1203 浏览 8 评论 0原文

我需要查看状态机的自定义发布事件是否被接受。我的方法是子类化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖正好 2024-10-21 07:32:42

好吧,最后回答我自己的问题:它有效,但基本上是一个黑客,因为没有正式记录。

Ok, finally answering my own question: It works but is basically a hack since not officially documented.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文