Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Widget Set Value to an Object then Update the Value to Another Widget
QtWS25 Last Chance

Widget Set Value to an Object then Update the Value to Another Widget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 321 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mimamrafi
    wrote on last edited by mimamrafi
    #1

    Hi everyone, I have a question. This is my goals schema

    Schema.png

    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

    JonBJ jsulmJ 2 Replies Last reply
    0
    • M mimamrafi

      Hi everyone, I have a question. This is my goals schema

      Schema.png

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @mimamrafi
      You have two instances of SerialReadWrite serialRW, one in maingui and another in channelframe. 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.

      1 Reply Last reply
      2
      • M mimamrafi

        Hi everyone, I have a question. This is my goals schema

        Schema.png

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved