Updating c++ data in Qml
-
@dziko147 said in Updating c++ data in Qml:
In my situation , when I would like to create an Backend instance in UploadCSV to emit signal .
And create Backend instance in MainWindow to set a contextProperty .
what should I do .
Please :)You should first explain why you think you have to create a Backend instance in UploadCSV?
It is impossible to understand (for me) what is the purpose of this.
And emitting a signal which is connected to nothing, results to nothing. -
@KroMignon in uploadcsv ui , i have a button . I want to emit clicked signal connected to a slot in backend which change data (see my code please) .
-
@dziko147 said in Updating c++ data in Qml:
in uploadcsv ui , i have a button . I want to emit clicked signal connected to a slot in backend which change data (see my code please) .
Your code is incomprehensible!
Please learn C++ basics, like what are classes, class members, class methods and class instances.I don't understand what is your problem, on click event the method
status()
instanceback
which is created inUploadCSV.cpp
will be called and you should see a debug message on console.I don't understand why you expect anything on QML side as this instance is not exposed to QML.
-
@dziko147 said in Updating c++ data in Qml:
there is no one who did something like this ?
Again, explain what you want to do, not what you have done.
Because what you have done don't made sense.If you want to update the
Backend
instanceback
create inMainWindow
on button click inUploadCSV.cpp
, then you have to connect the click event with this instance, not create a new instance ofBackend
inUploadCSV.cpp
. -
@KroMignon if I understand you . I should create an MainWindow Instance in UploadCSV.cpp then I call the back defined in MainWindow ?
Is that ? -
@dziko147 said in Updating c++ data in Qml:
if I understand you . I should create an MainWindow Instance in UploadCSV.cpp then I call the back defined in MainWindow ?
Is that ?No, you don't understand anything, and that is very bad :(
You have to connect the click signal from theUploadCSV
instance to theback
variable (which should be a class member ==> https://www.learncpp.com/cpp-tutorial/classes-and-class-members/) of theMainWindow
instance.And NOT create a new instance.
-
@dziko147 said in Updating c++ data in Qml:
Is that ?
No.
Why should UploadCSV create main window?!
Please read more carefully.You should really learn what instances are! Two different instance are exactly that: two different instances/objects, they are NOT same.
If you create two instances of a class (lets name them A and B), then connect a slot from A to a signal, then do you expect that the slot in B will also be called if the signal is emitted? Yes or No? -
@jsulm Sure No .
@KroMignon If I don't create MainWindow instance, how can i access to mainwindow variable from UploadCSV.
MainWindow *mymain=new MainWindow(); connect(ui->parambtn_2, SIGNAL(clicked()),mymain->back, SLOT(status()),Qt::QueuedConnection);
this is what I think .
Correct me please :) -
@dziko147 said in Updating c++ data in Qml:
If I don't create MainWindow instance, how can i access to mainwindow variable from UploadCSV.
Don't create a SECOND MainWindow instance! Use the one you already have (you probably create it in main.cpp, right?)...
-
-
@KroMignon Is this code correct ??
I create a class member :
class MainWindow : public QMainWindow { Q_OBJECT public: Backend *back;
then in uploadCSV.cpp
connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);
But still doesn't work !
-
@dziko147 said in Updating c++ data in Qml:
connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);
You're creating a new MainWindow instance in the connect.
I give up... -
@dziko147 You should really learn C++ basics first before you continue with Qt.
This is now second thread from you where people are trying to explain to you what you are doing wrong. Your problem is not Qt but missing C++ knowledge.For example: why did you write "MainWindow()" bellow? Do you know what MainWindow() does? And how does this relate to what I wrote (use existing MainWindow instance)? You again create a new MainWindow instance, even if you was told many times to not to do so!
connect(ui->parambtn_2, SIGNAL(clicked()),MainWindow().back, SLOT(statuspican()),Qt::QueuedConnection);
-
@dziko147 said in Updating c++ data in Qml:
Yes I understand that i create a second instance
You need to understand that it is quite disappointing for others to repeat the same thing again and again and again and then see that you are still doing EXACTLY the same mistake even if you know that it is wrong! So, why do you again post code which is wrong if you know that it is wrong?
I can't tell you how exactly you need to do it because I don't have your code and so don't know how it is structured...
From architectural point of view it is strange why uploadCSV should connect a signal to a slot in a member variable in another class? uploadCSV should not even know that MainWindow has a member called "back". This was already pointed out by @SGaistWhere is the uploadCSV instance created? Does MainWindow create it?
-
@jsulm this is my situation.
My project compouned of 3 classes :
1/Mainwindow
2/backend
/UploadCSV
In Mainwindow :
1/ I have a quickwidget which integrate a qml file .
2/ I create a backend instance to get data from it and fill the QMl FileIn Backend
1/I have the defenition of my data (getter, setter, change slots).In UploadCSV
1/I have a button when it's clicked a slot in Backend will change some data .
2/After change data I would like to display it on QML .I hope it's clear