[英]Give subclass of QQuickItem pointer to another C++ object
我非常希望將Qt QQuickItem類子類化以用於QML,因此在幕后給它提供了很多C ++邏輯。 但是,由於QML將實例化它並擁有它,而不是C ++端,我不知道如何(如果可能的話)在這個新對象和系統中的其他C ++對象之間傳遞指針或連接。 這可能嗎?
您可以在main.cpp
注冊一個全局QML對象,該對象會對任意QObject進行操作。
Brain *brain = new Brain();
QQmlApplicationEngine engine;
QQmlContext *context = engine.rootContext();
context->setContextProperty("brain", brain);
現在,您可以在QML中全局使用brain
。 例如,您可以將其傳遞給自定義組件
VisibleComponent {
id: vico1
width: 300
height: 300
Component.onCompleted: {
vico1.setLogic(brain)
}
}
這需要一個Q_INVOKABLE void setLogic(Brain* brain);
在VisibleComponent
。
我有一個正在運行的代碼項目,請告訴我你是否需要它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.