PDA

View Full Version : How to display a QGroupBox and its contents in two separate QWidgets?



quimnuss
7th October 2015, 10:34
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/7042799/showing-the-same-qpushbutton-on-multiple-widgets

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

d_stranz
7th October 2015, 18:41
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.

quimnuss
8th October 2015, 12:30
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.

anda_skoa
8th October 2015, 16:23
One option is to keep the data in UI less object and have both groupboxes just display that object's data.

Cheers,
_