QT QML 更改状态 c++

发布于 2024-11-09 06:47:29 字数 760 浏览 0 评论 0原文

我有一个调用信号的 MouseArea, 我将此信号插入插槽并且它正在工作,我的 C++ 代码正在运行。

但是在 C++ 代码中是否可以更改 QML 状态?

调用信号的按钮代码(OK):

MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        inscriptionCarre.qmlSignalButtonInscription("Button");
                    }
                }

我的状态代码:

states: [
    State {
        name: "start";
        PropertyChanges { target: home; x: -master.width; }
        PropertyChanges { target: login; x:0; }
    },
    State {
        name: "loginOK";
        PropertyChanges { target: login; x: -master.width; }
        PropertyChanges { target: liste; x:0; }
    }
]

我想在我的插槽(C++代码)内将状态更改为“loginOK”,这可能吗?

谢谢

i've a MouseArea that call a signal,
i plug this signal to a slot and it's working, my c++ code is running.

But is it possible inside c++ code, change the QML state ?

code of button that call signal (OK) :

MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        inscriptionCarre.qmlSignalButtonInscription("Button");
                    }
                }

Code of my states :

states: [
    State {
        name: "start";
        PropertyChanges { target: home; x: -master.width; }
        PropertyChanges { target: login; x:0; }
    },
    State {
        name: "loginOK";
        PropertyChanges { target: login; x: -master.width; }
        PropertyChanges { target: liste; x:0; }
    }
]

I would like inside my slot (c++ code) change state to "loginOK", is it possible ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

放手` 2024-11-16 06:47:29

由于状态是一个项目属性,因此您应该能够像这样修改它:

QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, "MyItem.qml");
QObject *object = component.create();
object->setProperty("state", "loginOK");

参考:http://qt-project.org/doc/qt-4.8/qtbinding.html#modifying-properties

或者从您的 C++ 插槽中,您可以发出一个信号,将状态字符串传递到 QML 插槽这反过来又设置了状态。例如:

C++ 文件:

...
signals:
     void stateChanged(const QString &newState);
...

QML 文件:

...
MyItem {
     onStateChanged: {
         state: newState
     }
}
...

Since state is an item property, you should be able to modify it like so:

QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, "MyItem.qml");
QObject *object = component.create();
object->setProperty("state", "loginOK");

Reference: http://qt-project.org/doc/qt-4.8/qtbinding.html#modifying-properties

Or from your C++ slot you could emit a signal that passes the state string to a QML slot that in turn sets the state. For example like:

C++ file:

...
signals:
     void stateChanged(const QString &newState);
...

QML file:

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