简体   繁体   English

从 C++ 函数更改 QML 标签文本

[英]Change QML Label text from a C++ function

I have a Label in QML and I want to change its text value when I click on a button.我在 QML 中有一个标签,我想在单击按钮时更改其文本值。 I have tried many different ways to achieve this, but nothing seems to work properly.我尝试了许多不同的方法来实现这一目标,但似乎没有任何方法可以正常工作。 I have used QObject::setProperty() and it seems to work when I print the new text value with qDebug(), but it does not refresh on the GUI.我使用了 QObject::setProperty(),当我使用 qDebug() 打印新文本值时它似乎可以工作,但它不会在 GUI 上刷新。 What am I doing wrong?我究竟做错了什么?

main.cpp:主.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QSettings>
#include <QQuickStyle>
#include <QtQuickControls2>
#include <QQmlContext>
#include <QIcon>

#include "Controllers/Network/network.hpp"
#include "Controllers/NFC/nfc.hpp"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QIcon::setThemeName("gallery");
    QQuickStyle::setStyle("Material");

    // Property bindings:
    qmlRegisterType<RFS::Communication::Network>("rfs.communication.network", 1, 0, "Network");
    qmlRegisterType<RFS::Communication::NFC>("rfs.communication.nfc", 1, 0, "NFC");

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("availableStyles", QQuickStyle::availableStyles());
    engine.load(QUrl("qrc:/main.qml"));
    if (engine.rootObjects().isEmpty()) return -1;
    return app.exec();
}

nfc.hpp: NFC.hpp:

#include <QObject>
#include <QtNfc/qnearfieldmanager.h>
#include <QtNfc/qnearfieldtarget.h>

namespace RFS::Communication
{
    class NFC : public QObject
    {
        Q_OBJECT

    public:
        explicit NFC(QObject *parent = nullptr);
        Q_INVOKABLE bool getStatus() { return pairingStatus; }
        Q_INVOKABLE void changeTextValue();

    private:
        bool pairingStatus;
    };
}

nfc.cpp: NFC.cpp:

#include <QtQuick>
#include <QQuickView>
#include "Controllers/NFC/nfc.hpp"

void RFS::Communication::NFC::changeTextValue()
{
    QQuickView view;
    view.setSource(QUrl("qrc:/Views/overview.qml"));
    QObject *rootObject = view.rootObject();

    QList<QObject*> list = rootObject->findChildren<QObject*>();
    QObject *testLabel = rootObject->findChild<QObject*>("testLabel");

    qDebug() << "Object" << testLabel->property("text"); // Successfully prints old value
    testLabel->setProperty("text", "test1");
    qDebug() << "Object" << testLabel->property("text"); // Successfully prints new value
    QQmlProperty(testLabel, "text").write("test2");
    qDebug() << "Object" << testLabel->property("text"); // Successfully prints new value
}

overview.qml:概述.qml:

import QtQuick 2.12
import QtQuick.Controls 2.12
import rfs.communication.nfc 1.0

Page {
    id: page

    NFC {
        id: nfc
    }

    SwipeView {
        id: swipeView
        anchors.fill: parent
        currentIndex: tabBar.currentIndex

        Pane {
            id: overviewTab
            width: swipeView.width
            height: swipeView.height

            Button {
                id: pairButton
                text: qsTr("Pair new receiver")

                onClicked: {
                    nfc.changeTextValue()
                }
            }

            Label {
                id: testLabel
                objectName: "testLabel"
                text: "hei" // I want to change this value
            }
        }
    }
}

Is there any better way to achieve this?有没有更好的方法来实现这一目标? Thanks a lot in advance.非常感谢。

Anyone looking for a simple solution, I just came up with this trying to achieve changing label's text on c++ side by pressing a button in QML任何寻找简单解决方案的人,我只是想出了这个尝试通过按下 QML 中的按钮来实现在 C++ 端更改标签文本的方法

Add to main.cpp:添加到 main.cpp:

Backend backend;
engine.rootContext()->setContextProperty("backend", &backend);

Create a new class (backend.h & backend.cpp)创建一个新类(backend.h & backend.cpp)

In backend.h:在后端.h:

#ifndef CONTROLLERS_H
#define CONTROLLERS_H

#include <Qt>
#include <QObject>
#include <QCoreApplication>
#include <QWindow>
#include <QString>
    class Backend : public QObject
{
    Q_OBJECT
public:
    explicit Backend(QObject *parent = nullptr);
   
public slots:
    void setText();
    QString getText();
};
#endif // CONTROLLERS_H

In backend.cpp:在 backend.cpp 中:

#include "backend.h"

QString text = ""; // don't forget this
Backend::Backend(QObject *parent) : QObject(parent)
{

}

QString Backend::getText()
{
    return text; //returns the text you set or didnt in setText()
}

void Backend::setText()
{
    text = "text"; // or do anything you want with global QString text
}

In QML:在 QML 中:

Label{
    id: myLabel
}

Button{ 
    id: myButton
    onClicked:{
        backend.setText()
        myLabel.text = backend.getText()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM