如何从 C++ 修改 QML 文本

发布于 2024-12-29 15:06:22 字数 1650 浏览 0 评论 0原文

我是 Qt 新手,我正在尝试从 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
    }

}

有人可以帮助我吗?

I'm new to Qt and I'm trying to modify a QML Text (showed in the screen) from the C++ code.
I get the text modified but it is not updated on the screen, so I have the text variable modified but the first text on the screen.

Here is the code:

//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
    }

}

Could someone help me?

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

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

发布评论

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

评论(1

﹎☆浅夏丿初晴 2025-01-05 15:06:22

这可能不像使用 objectName 属性查找对象那么灵活,但这很简单。

main.cpp

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QGraphicsObject>


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

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/TextTest/main.qml"));
    QObject *rootObject = viewer.rootObject();
    rootObject->setProperty("text1Text",QVariant("Change you text here..."));

    viewer.showExpanded();
    int returnVal = app.exec();
    delete rootObject;
    return returnVal;
}

main.qml

import QtQuick 1.0

Item {
    id: item1
    width: 400
    height: 400
    property alias text1Text: text1.text

    Text {
        id: text1
        width: 400
        height: 29
        color: "red"
        text: "This text should change..."
        font.pixelSize: 12
    }

}

This may not be as flexible as finding the object using the objectName property, but this will be simple.

main.cpp

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QGraphicsObject>


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

    QmlApplicationViewer viewer;
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/TextTest/main.qml"));
    QObject *rootObject = viewer.rootObject();
    rootObject->setProperty("text1Text",QVariant("Change you text here..."));

    viewer.showExpanded();
    int returnVal = app.exec();
    delete rootObject;
    return returnVal;
}

main.qml

import QtQuick 1.0

Item {
    id: item1
    width: 400
    height: 400
    property alias text1Text: text1.text

    Text {
        id: text1
        width: 400
        height: 29
        color: "red"
        text: "This text should change..."
        font.pixelSize: 12
    }

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