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 -
@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_ELEMENTpublic:
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 :)
-
#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);