You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QT QML报错:无法在状态定义中应用状态变更的排查求助

QT QML错误:can't apply a state change as part of a state definition

问题详情

当C++导出到QML的对象testBack的自定义信号onPrintDataReady触发后,尝试切换到printState时出现该错误,其余3种状态切换逻辑均正常工作。

已确认onPrintDataReady信号可正常触发(通过修改矩形颜色的测试验证有效),尝试过以下方法但均未解决:

  • 将信号连接直接写在testBack项中
  • 开启STATECHANGE_DEBUG调试模式

网上关于该错误的相关信息极少,特此求助。

相关代码片段

信号连接逻辑

Connections {
    target: timer
    function onTriggered() {rectangle.state = "welcomeState"}
}

Connections {
    target: testBack
    function onPrintDataReady() {rectangle.state = "printState"}
}

Connections {
    target: boutonOui
    function onPressed() {rectangle.state = "endState";testBack.printFile() }
}

Connections {
    target: boutonNon
    function onPressed() {rectangle.state = "endState"}
}

状态定义

states: [

    State {
        name: "welcomeState"
        
        PropertyChanges {
            target: logo_2
            visible: false
        }

        PropertyChanges {
            target: messageFin
            visible: false
        }

        PropertyChanges {
            target: boutonOui
            visible: false
        }

        PropertyChanges {
            target: boutonNon
            visible: false
        }

        PropertyChanges {
            target: impressionTicket
            visible: false
        }
    },

    State {
        name: "printState"

        PropertyChanges {
            target: boutonOui
            visible: true
            text: qsTr("OUI")
        }

        PropertyChanges {
            target: boutonNon
            visible: true
            text: "NON"
        }

        PropertyChanges {
            target: logo_1
            visible: false
        }

        PropertyChanges {
            target: impressionTicket
            visible: true
        }

        PropertyChanges {
            target: logo_2
            visible: true
        }

        PropertyChanges {
            target: messageFin
            visible: false
            text: qsTr("Merci de votre visite. A bientôt.")
        }
    },

    State {
        name: "endState"
        
        PropertyChanges {
            target: logo_1
            visible: false
        }

        PropertyChanges {
            target: logo_2
            visible: true
        }

        PropertyChanges {
            target: messageFin
            visible: true
            text: qsTr("Merci de votre visite. A bientôt.")
            anchors.verticalCenterOffset: 1
            anchors.horizontalCenterOffset: 1
        }

        PropertyChanges {
            target: boutonNon
            visible: false
            text: qsTr("NON")
        }

        PropertyChanges {
            target: impressionTicket
            visible: false
        }

        PropertyChanges {
            target: boutonOui
            visible: false
        }

        PropertyChanges {
            target: timer
            running: true
            triggeredOnStart: false
            interval: 6000
        }
    }
]

初始状态设置

Rectangle {
    id: rectangle
    width: 800
    height: Constants.height
    color: "#eb5a2d"
    state: "welcomeState"
    property alias messageFin: messageFin
    property alias logo_1: logo_1
    property bool property0: true
}

内容的提问来源于stack exchange,提问作者alassaneg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 15:05:32