Widget Set Value to an Object then Update the Value to Another Widget
-
Hi everyone, I have a question. This is my goals schema
Example : I want to set value from widget 'a' and update the value in object, then I call the updated values from other widget
This is the first widget :
//maingui.h ... #include "serialreadwrite.h" public : void settingValue (); ... private: Ui::MainGUI *ui; SerialReadWrite serialRW;
//maingui.cpp void MainGUI::settingValue () { ... serialRW.setValue(5); }
This is the object class :
//serialreadwrite.h class SerialReadWrite : public QObject { public: void setValue(int value); int getValue(); private : int currentValue = 0;
//serialreadwrite.cpp void SerialReadWrite::setValue(int value) { currentValue = value; } int SerialReadWrite::getValue() { return currentValue; }
this is the second widget (another widget)
//channelframe.h #include "serialreadwrite.h" public : void gotVal(); ... private: Ui::ChannelFrame *ui; SerialReadWrite serialRW;
//channelframe.cpp void ChannelFrame::gotVal() { qDebug () << serialRW.getValue(); //The values I got is 0, not 5 :( }
However, I still get 0 (it's like I make new QObject again). I'm sorry for my little knowledge. is there any clues for my issues ? Thankyou for your help
-
@mimamrafi
You have two instances ofSerialReadWrite serialRW
, one inmaingui
and another inchannelframe
. These are not related, they are not the same object/instance. ("it's like I make new QObject again" --- indeed you do!) If they are supposed to be you need to create a unique instance somewhere and refer to that same instance elsewhere. -
@mimamrafi said in Widget Set Value to an Object then Update the Value to Another Widget:
SerialReadWrite serialRW;
You have two different SerialReadWrite instances in your classes which do not have anything to do with each other.
You should use signals/slots in Qt applications: class A emits a signal if the value is changed. This signal is connect to a slot in Object class, in that slot you can also emit a signal and connect a slot from class B to it. This way you have a flexible architecture: you can have many connections between class A nad Object class and also many connections between Object class and class B.