How to update a QML text with c++
Asked Answered
D

1

2

I have a small problem. Can someone show me how can I update a qml text from c++. I have an example using threads but I don't want to apply this method because I don't know how to set a parameter in run() function.To fully understand me here is my code.In the main function when I start the thread I want to put my custom text or a string variable that has a text.

thread.h

#ifndef THREAD_H
#define THREAD_H

#include <QThread>

class Thread : public QThread
{
    Q_OBJECT
public:
    Thread(QObject *parent=nullptr);
   ~Thread() override;
    Q_SLOT void stop();
    Q_SIGNAL bool textChanged(const QString & text);
protected:
    void run() override;
};

#endif // THREAD_H

thread.c

#include "thread.h"
#include <QDebug>

Thread::Thread(QObject *parent):
    QThread(parent)
{
}

Thread::~Thread()
{
}

void Thread::stop()
{
    requestInterruption();
    wait();
}

void Thread::run() //here I want to specify a QString variable such that in main function to call the function with my text, and not specified the text from here
{
int i=0;

while(!isInterruptionRequested()){
QString text;
    text = QString("I changed the text"); // I don't want to declare from here the text.
        Q_EMIT textChanged(text);
QThread::msleep(20);
    qDebug() <<i++;

  }
}

main.cpp

...
Thread thread;
    QQmlApplicationEngine engine;
QObject::connect(&app, &QGuiApplication::aboutToQuit, &thread, &Thread::stop);
            thread.start();
            engine.rootContext()->setContextProperty("thread", &thread);
            engine.load(QUrl("qrc:/main.qml"));
            thread.stop();
...

main.qml

  .....
  Text {
                    objectName: "myLabel"
                    id: txt
                    width: 200
                    height: 29
                    color: "#faf9f9"
                    text: qsTr("Text")
                    font.pixelSize: 12
                    }
        Connections{
                target: thread
                onTextChanged: txt.text = text
        }

.....
Damselfish answered 25/12, 2019 at 18:31 Comment(0)
K
3

You can send data from C++ to qml using signals, like this:

//main.cpp

int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    // Class init
    YourClass yourObject;

    // Embedding C++ Objects into QML with Context Properties
    QQmlContext* ctx = engine.rootContext();
    ctx->setContextProperty("yourObject", &yourObject);

    return app.exec();
    }

//main.qml

import QtQuick 2.6

Window {
    id: mainWindow

    Connections {
        target: gapi
        onSignalData: {
            console.log("Data: " + data)
            textToChange.text = "Changed to: " + data
        }
    }

    Text {
        id: textToChange
        text: "beforeChange"
    }
}

//yourClass.h

class YourClass : public QObject
{
signals:
    // Signal from YourClass to QML
    void signalData(QString data);
}

//yourClass.cpp

emit signalData("Hello QML"); // Signal from GAPI to QML

This page https://felgo.com/cross-platform-development/how-to-expose-a-qt-cpp-class-with-signals-and-slots-to-qml have a complete tutorial about "How to Expose a Qt C++ Class with Signals and Slots to QML"

Kowatch answered 27/12, 2019 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.