Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

problem with getting data from c++ to qml using QtQuickWidget



  • Hello ,
    Currently I displayed data from c++ to Qml QQuickwidget using signal and slots .
    I used this connection :
    QObject::connect(rootObject, SIGNAL(qmlVoltage()),
    this, SLOT(onSwitchVoltageClicked()),Qt::QueuedConnection);

    first problem: I would like to change this data from another Qdialog , when i change data there is no update in my qmlfile .

    Also I tried to display different qml file in the same Qquickwidget using signal and slots .
    Second problem: when i display the second qml file i get this error :

    TypeError: Cannot read property 'Voltage' of null .

    I will appreciate any suggestion .
    thank you



  • @dziko147 there are a few steps involved in getting C++ data to your QML. Here's a good start to understanding this.

    Since you didn't post any code, we can't tell how much of this setup work you've done.


  • Moderators

    Hi @mzimmers, your link is missing; could you please edit it in?



  • @mzimmers thanks for your reply :)
    Currently i use this code :

    #ifndef BACKEND_H
    #define BACKEND_H

    #include <QObject>
    #include <QQmlEngine>

    class Backend : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString directiontext READ getdirectiontext WRITE setdirectiontext NOTIFY directiontextChanged)
    QML_ELEMENT

    public:
    explicit Backend(QObject *parent = nullptr);
    QString getdirectiontext() {return m_directiontext;}
    ;}

    }
    

    //Setter methode //

    void setdirectiontext(QString text);
    

    signals:
    void directiontextChanged();

    private :
    QString m_directiontext;

    };

    #endif // BACKEND_H

    //Backend.cpp
    #include"backend.h"
    #include<QDebug>
    Backend::Backend(QObject *parent ) :QObject(parent)
    {
    m_directiontext="test direct";

    void Backend::setdirectiontext(QString text)
    {
    qDebug("signal direction called");
    if(m_directiontext == text)
    return;
    m_directiontext = text;
    emit directiontextChanged();
    }

    //MainWindow
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow),

    {

    Backend back ;
    m_ui->quickWidget->rootContext()->setContextProperty("cppObject",&back);

    //In my Qml file :
    text: qsTr(cppObject.directiontext)

    //then i would like to change the data by :
    Backend back;
    QString Mode= ui->modecommande->currentText();
    qDebug() << Mode << "this is Mode select commande" ;
    back.setdirectiontext(Mode);

    I hope that my question is clear :)


  • Lifetime Qt Champion

    @dziko147 Please format your code properly, it makes it easier for others to read and understand it.



  • #ifndef BACKEND_H
    #define BACKEND_H
    
    #include <QObject>
    #include <QQmlEngine>
    
    class Backend : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(QString directiontext READ getdirectiontext WRITE setdirectiontext NOTIFY directiontextChanged)
    QML_ELEMENT
    
    public:
    explicit Backend(QObject *parent = nullptr);
    QString getdirectiontext() {return m_directiontext;}
    ;}
    
    }
    //Setter methode //
    
    void setdirectiontext(QString text);
    signals:
    void directiontextChanged();
    
    private :
    QString m_directiontext;
    
    };
    
    #endif // BACKEND_H
    
    //Backend.cpp
    #include"backend.h"
    #include<QDebug>
    Backend::Backend(QObject *parent ) :QObject(parent)
    {
    m_directiontext="test direct";
    
    void Backend::setdirectiontext(QString text)
    {
    qDebug("signal direction called");
    if(m_directiontext == text)
    return;
    m_directiontext = text;
    emit directiontextChanged();
    }
    
    //MainWindow
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow),
    
    {
    
    Backend back ;
    m_ui->quickWidget->rootContext()->setContextProperty("cppObject",&back);
    
    //In my Qml file :
    text: qsTr(cppObject.directiontext)
    
    //then i would like to change the data by :
    Backend back;
    QString Mode= ui->modecommande->currentText();
    qDebug() << Mode << "this is Mode select commande" ;
    back.setdirectiontext(Mode);
    

Log in to reply