gpt4 book ai didi

c++ - 从 C++ 插槽更改 QML 对象

转载 作者:行者123 更新时间:2023-11-28 05:35:41 25 4
gpt4 key购买 nike

我想从 C++ 中的插槽更改 QML 中定义的对象。在插槽 startButtonClicked() 中,我启动计时器,每秒调用插槽 getData()。如何从 C++ 插槽 genData() 更改 QML 中定义的标签?现在我只能从 main.cpp 进行更改

class LogicClass : public QObject
{
Q_OBJECT
public:
LogicClass();
~LogicClass();

public slots:
void startButtonClicked(const QVariant &v);
void getData();
};

主要:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

class LogicClass logicClass;

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

QObject *rootObject = engine.rootObjects().first();
QObject *qmlObject = rootObject->findChild<QObject*>("startButton");

QObject::connect(qmlObject, SIGNAL(qmlSignal(QVariant)),&logicClass, SLOT(startButtonClicked(QVariant)));

return app.exec();
}

qml:

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.2

ApplicationWindow {
id: window
objectName: "window"
visible: true
width: 640
height: 520
title: qsTr("MY app")
Button {
id: startButton
objectName: "startButton"
x: 25
text: qsTr("Start")

signal qmlSignal(var anObject)

MouseArea {
anchors.fill: parent
onClicked: startButton.qmlSignal(startButton)
}
}
Label {
objectName: "latitudeLabelValue"
id: latitudeLabelValue
y: 478
width: 50
text: qsTr("")
}

}

}

最佳答案

您必须使用 setProperty 方法:

    QObject *lblLatitute = rootObject->findChild<QObject*>("latitudeLabelValue");

lblLatitute->setProperty("text", "234.234");

但考虑使用模型/ View /委托(delegate)范式。

关于c++ - 从 C++ 插槽更改 QML 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38346606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com