PDA

View Full Version : How to display the same widget in all tabs of a QTabWidget



dictoon
8th January 2010, 15:56
Hello,

I need to display the same widget (really, the same instance of a widget) in all tabs of a QTabWidget. The idea is that I would change the content of this "shared" widget, based on the selected tab. I cannot instantiate this widget multiple times.

Is there a clean way to do this?

I considered the following options:

- Add the shared widget as a child to all tabs of the QTabWidget. I didn't pursue the idea, because from what I could gather, a widget can have only one parent.

- Use a QStackedWidget, put the shared widget on the first page, put the QTabWidget on the second page, and somehow make that second page transparent. I can foresee focus problems, though.

Any idea?

Cheers,
Franz

wysota
8th January 2010, 20:30
There is no clean way to do this. And I would really advise against any hackish ways of doing that. Why can't you instantiate the same widget class multiple times?

dictoon
11th January 2010, 10:29
Hey, thanks for the reply.

I was kind of expecting this to not be feasible cleanly, to be honest :)

I think letting the user switch the content of a widget using tabs makes sense semantically, but I understand it's preferable not to work around the framework.

Cheers,
Franz

wysota
11th January 2010, 10:52
You can have tabs without a tab widget, that's not a big problem.

vetgorky1
11th February 2010, 23:57
I have successfully done this by making the TabWidget (not the Tabs) the parent of a Label which has no background.

I then put a Layout onto the Label, and put various widgets into the label's layout. The widgets in the (invisible) Label layout then appear on all Tabs, and the Tab contents float beneath the Label's widgets.

I needed to shift the position of the Label away from (0, 0) to avoid the Tab buttons, but that wasn't a problem with move().

You need to make sure the label does not lie over anything you might want to click on in the Tabs - because it lets you see them, but not click them !

I usually work in code, but tried this out in Designer first - didn't take long.

I am now trying to put another invisible Label at the bottom of the TabWidget, but am having difficulty getting an accurate value for height(), since this is not the same during coding as it is at runtime! I guess I will have to use resize events etc for this.

Hope this is of some use!

Coises
12th February 2010, 08:09
I need to display the same widget (really, the same instance of a widget) in all tabs of a QTabWidget. The idea is that I would change the content of this "shared" widget, based on the selected tab. I cannot instantiate this widget multiple times.

Is there a clean way to do this?

Use a QTabBar instead of a QTabWidget.