Unsolved problem with updating c++ data in a QML file
-
Hello , I want to update displayed data in Qml from C++ classes .
So what I did is :1/ set a Contextproperty
m_ui->quickWidget->rootContext()->setContextProperty("cppObject",&back);
2/ create signal/slot
connect(this, SIGNAL(directchange(QString)),&back, SLOT(Directchanged(QString)),Qt::QueuedConnection);
3/ emit signal
Mode= ui->modecommande->currentText(); qDebug() << Mode << "this is Mode select commande" ; emit directchange(Mode);
4/ the slot :
void Backend::Directchanged(QString text) { qDebug() << "text recu de commande " << text; setdirectiontext(text); qDebug() << "currently m-direct "<< m_directiontext; // here I get the exact QString Value }
in m_directiontext; I get the exact value but the QmlFile doesn't update the value
-
@dziko147 where is
back
defined? -
@KroMignon based on his other thread I would say locally in the constructor.
-
@KroMignon @SGaist No . Back is a Global variable defined in command.h (the headers where the connect is defined) .
-
@dziko147 said in problem with updating c++ data in a QML file:
Back is a Global variable
Global variables should be avoided as much as possible. Usually they are not needed and are a sign for bad design.
-
@dziko147 said in problem with updating c++ data in a QML file:
No . Back is a Global variable defined in command.h (the headers where the connect is defined) .
Global variable defined in header sounds strange to me.
And, as @jsulm already wrote, in C++ there is no good reason to use global variable... In C it may be used, but in C++ avoid it!I don't know what are your skills in programming, but you don't seems to be an experimented programmer.
Why do you not start with Qt examples and try to understand how it works?
Then start to modify it to adapt it to your needs.Please take time to read this, at least: https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html