3
Qt를 처음 사용하고 있으며 C++ 코드에서 QML 텍스트 (화면에 표시된 것)를 수정하려고합니다. 텍스트가 수정되었지만 화면에 업데이트되지 않았으므로 텍스트 변수가 수정되었지만 화면의 첫 번째 텍스트는 수정되었습니다.C++에서 QML 텍스트를 수정하는 방법
//main.cpp
#include <QApplication>
#include <QDeclarativeEngine>
#include <QDeclarativeComponent>
#include <QDeclarativeItem>
#include <QDebug>
#include "qmlapplicationviewer.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/textModification/main.qml"));
viewer.showExpanded();
QDeclarativeEngine engine;
QDeclarativeComponent component(&engine, QUrl::fromLocalFile("qml/textModification/main.qml"));
QObject *object = component.create();
QObject *item = qobject_cast<QDeclarativeItem*>(object);
QObject *text = item->findChild<QObject*>("text1");
qDebug() << "Text of 'text1' when it's created' -------->" << text->property("text");
text->setProperty("text", "THIS WORKS!");
qDebug() << "Text of 'text1' after modifying it -------->" << text->property("text");
return app->exec();
}
//main.qml
import QtQuick 1.0
Item {
id: item1
objectName: "item1"
width: 400
height: 400
Text {
id: text1
objectName: "text1"
x: 0
y: 0
width: 400
height: 29
text: "This text should change..."
font.pixelSize: 12
}
}
누군가가 나를 도울 수 : 여기
코드인가?감사합니다.
당신이 항목에 대한 업데이 트를()를 호출하려고 했습니까? 또한 텍스트를 C++ QObject의 속성으로 QML (QDeclarativeContext의 setContextProperty, QML의 "object.propertyname")으로 내보내는 것이 더 쉬울 수도 있습니다. –
텍스트 객체에 대한 update() 호출이 없습니다. 나는 당신이 나에게 말한 방식으로 수출하려고 노력할 것이고 나는 당신에게 말할 것입니다. 감사합니다. – AZorrozua
QObject 대신 QGraphicsObject로 캐스팅을 시도하십시오. –