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. parent-child communication
Qt 6.11 is out! See what's new in the release blog

parent-child communication

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 739 Views 2 Watching
  • 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.
  • H Offline
    H Offline
    harry
    wrote on last edited by
    #1

    hi.
    i have myScreen and myButton, both subclasses of QWidget. the button is a child of the screen. they need to exchange information. how can i get them to communicate?
    i'm thinking something like this:

    screen: i want you to be color number 2.
    button: ok.

    button: erm, what is color number 2?
    screen: green.
    button: ok, got it.

    IOW. the button needs to be able to request info from the screen. but how the **** do i do that? every approach i tried has resulted in error messages.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Usually it's done along the lines of:

      
      class IInfoProvider {
      public:
         ...
         virtual QColor getColor(int idx) = 0;
      }
      
      class Button {
      public:
         ...
         void setColor(int idx, IInfoProvider* ip) { setColorSomehow(ip->getColor(idx));  }
      }
      
      class Screen : public IInfoProvider {
         Button myButton;
      public:
         ...
         QColor getColor(int idx) override {  return colorFromIndexSomehow(idx);   }
      
         void doStuff() { myButton.setColor(2, this);  }
      }
      
      
      Screen myScreen;
      myScreen.doStuff();
      
      1 Reply Last reply
      0

      • Login

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