Results 1 to 4 of 4

Thread: How to display a QGroupBox and its contents in two separate QWidgets?

  1. #1
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to display a QGroupBox and its contents in two separate QWidgets?

    I have a QGroupBox with three QLabels. How could I show a replica of the QGroupBox in a windowed widget and the MainWindow as well?

    I know I could emit its contents to keep the sync but is there another way?

    I also know I could re-implement the PaintEvent and render it twice as shown here:
    http://stackoverflow.com/questions/7...ltiple-widgets

    But I don't like it so much, I would be breaking the decoupled design.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to display a QGroupBox and its contents in two separate QWidgets?

    That stackoverflow post is really an ugly hack, IMO. What exactly are you trying to keep in sync? The text or images on the labels? How are they being changed at run time?

    What I would do is something like this: Create a custom QWidget-derived class containing your group box and its labels, and add instances of this class to your windowed widget and main window. For this widget, add a signal ("contentsChanged" or something like that) that has as its argument a vector of QStrings, QPixmaps, or whatever it is you want kept in sync. When any of the labels is changed, emit the signal with the updated vector. Also implement a slot ("onContentsChanged) that has the same vector as an argument. In this slot, update the labels with the information passed in.

    When you construct the two widget instances, cross-connect their signals and slots so that when one changes, the other gets updated.

    You have to be careful to avoid recursion - do not emit the contentsChanged() signal in the onContentsChanged() slot or you'll get into an infinite recursion.

  3. #3
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to display a QGroupBox and its contents in two separate QWidgets?

    Both, images and text.

    I've reached the same conclusion and I've done something similar to what you suggest. Since one groupbox is the slave, everytime I change the value of the contents of the main groupbox I send a signal to the other container. It's less object-oriented as your solution, for which I'd say your solution is better.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to display a QGroupBox and its contents in two separate QWidgets?

    One option is to keep the data in UI less object and have both groupboxes just display that object's data.

    Cheers,
    _

Similar Threads

  1. Replies: 8
    Last Post: 4th May 2015, 02:16
  2. How to display contents in Ui from another class
    By shivendra46d in forum Newbie
    Replies: 1
    Last Post: 19th August 2013, 11:37
  3. Replies: 3
    Last Post: 19th December 2011, 16:09
  4. Display contents of .txt file
    By karmo in forum Newbie
    Replies: 17
    Last Post: 23rd January 2010, 20:39
  5. Replies: 0
    Last Post: 9th April 2009, 06:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.