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

C++ data didn't update in Qml



  • Hello guys ,
    I have a frontend Qml , i want to display some data .
    What i do is i create a backend.cpp class

    #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();
    }

     
    I used a quickwidget to display Qml file 
    here is my Mainwindow 
    

    qmlRegisterType<Backend>("com.beckend", 1, 0, "Myback");

    m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));```

    and my qmlfile

    import com.beckend 1.0
    Myback{
        id:back
    }
    Label {
        id: label5
        x: 879
        y: 192
        text: back.directiontext
    }
    

    I have another class for change , so i create a command.cpp class and this is my code .

    Backend back;
    QString Mode= ui->modecommande->currentText(); // this is a combobox
    qDebug() << Mode << "this is Mode select commande" ;
    back.setdirectiontext(Mode);
    

    when the combobox value change the data should change in my qmlfile .

    Currently , i couldn't do this .

    Any help please
    thank you



  • @dziko147 said in C++ data didn't update in Qml:

    when the combobox value change the data should change in my qmlfile .
    Currently , i couldn't do this .

    This cannot work as it is.

    Backend back;
    QString Mode= ui->modecommande->currentText(); // this is a combobox
    qDebug() << Mode << "this is Mode select commande" ;
    back.setdirectiontext(Mode);
    

    This will create a local variable back updates m_directiontext and destroy the variable on function end.
    Why should QML be aware about this?


    import com.beckend 1.0
    Myback{
        id:back
    }
    

    Will create a QML component back, there is no link with C++ side.


    The right way is to create a global instance of Backend, and register it into QML context.

    qmlRegisterType<Backend>("com.beckend", 1, 0, "Myback");
    
    m_ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    // supposing m_ui has a member `m_back`
    m_ui->quickWidget->rootContext()->setContextProperty("backend", &m_back);
    m_ui->quickWidget->setSource(QUrl(QStringLiteral("qrc:/qml/automotive.qml")));```
    

    Use this reference in QML and then all changes on C++ will be available on QML side.

    Label {
        id: label5
        x: 879
        y: 192
        text: backend.directiontext
    }
    


  • @KroMignon thanks for your reply .
    I tried this solution and it doesn't work . :/



  • @dziko147 said in C++ data didn't update in Qml:

    I tried this solution and it doesn't work . :/

    Did you change also change the update method?

    QString Mode= ui->modecommande->currentText(); // this is a combobox
    qDebug() << Mode << "this is Mode select commande" ;
    ui->m_back.setdirectiontext(Mode);
    


  • This post is deleted!


  • @dziko147 said in C++ data didn't update in Qml:

    @KroMignon No ,
    Can you suggest a method ?

    I don't understand what you mean, please be more clear.
    No what?

    • No you don't have change the update method?
    • After changing the update method, the QML didn't be update?


  • @KroMignon the update method is in another class .
    So how can get this m_back :)
    i am really new in Qt developpment .
    So excuse meif my question is stupid :)



  • This post is deleted!


  • @dziko147 said in C++ data didn't update in Qml:

    the update method is in another class .
    So how can get this m_back :)
    i am really new in Qt developpment .
    So excuse meif my question is stupid :)

    I don't want to hurt you, but this has nothing to do with Qt, this is C++ basic.

    Do you have any knowledge about C++ programming?
    Do you know what is a class?
    Do you know what is a class method or member?

    Please take time to got basic knowledge about the programming language you want to use.
    There are many C++ tutorials available, take one and do at least the first step to understand what you are doing.

    Basic C++ knowledge is mandatory if you want to program with Qt which is a C++ framework.



  • @KroMignon ok i will :)
    but can you provide a way to create this m_back instance :)



  • @dziko147 said in C++ data didn't update in Qml:

    but can you provide a way to create this m_back instance :)

    https://www.w3schools.com/cpp/cpp_classes.asp



  • @KroMignon I know how to use class/object .
    I know how to manipulate c++ .
    But my problem is how can get this in my second class

    m_ui->quickWidget->rootContext()->setContextProperty("backend", &m_back);
    


  • @dziko147 said in C++ data didn't update in Qml:

    But my problem is how can get this in my second class

    I don't understand which second class you are talking about?



  • This post is deleted!



Log in to reply