tags:

views:

328

answers:

2

In my QT application I use a QTabWidget for the base navigation. This QTabWidget I setup in the ui. In some of the tabs of the QTabWidget I need to have QStackedWidget to be able to "drill down in the view".

I tried adding the QStackedWidget inside the ui also but it automatically adds a page to the stack. I want to add the pages for the QStackedWidget in code instead. If I in the code try to do this the stackedWidget already have a standard page so myWidget will be the second in the stack.

MyWidget *myWidget = new MyWidget(ui.stackedWidget);
ui.stackedWidget->addWidget(myWidget);

What is the best and easiest way to setup a QStackedWidget inside QTabWidget tab?

A: 

How about:

QTabWidget *myTabWidget = new QTabWidget(this);
QStackedWidget *myStackedWidget = new QStackedWidget(myTabWidget);

myTabWidget->addTab(myStackedWidget, "Stacked Widget");

Also you can remove all existing stack pages in Qt's Designer/Creator. Just right-click on the stacked widget and remove all existing pages. Then you can add the needed pages in the code using addWidget().

BastiBense
So you think I should create the QTabWidget in code also? I thought it was nice to create it in the ui but maybe that is not a good solution.
Martin
Also it seems in the designer when right-clicking it seems like I can delete all pages but one. So there will always be one page in the QStackWidget.
Martin
In Qt Creator I could also delete the last page. By the way, you can edit the .ui file with a text editor to remove that last page if you can't do so in the Designer. Also it might be a good idea to create a QTabWidget in the code, since it's not so much more work.
BastiBense
A: 

I'd say - create it in ui, just like you do (this way it's easier to layout/position, add other widgets on the tab later, etc), but simply remove all existing pages (added by designer) from code and add your new ones.

Actually Designer from Qt 4.6 allows to delete all pages from stacked widget - you need to right click, go to submenu "Page X of Y", and choose Delete. Repeat until all pages are gone :)

Maybe this got added to the Designer just recently, so you may still need to remove them from the code if you have an earlier version of Qt.

Speaking of keeping stuff inside ui against keeping it in code i'd vote for "as much in UI-file as possible" :)

dpimka
Yes I too vote for UI! :) But I use Carbide when building my application (QT for Symbian) and there it seems like I can't remove from the designer, or am I wrong?
Martin
Hmm, maybe you have the older version of Qt for Symbian?IIRC Qt 4.6.1 was released for symbian too - it should have the new Designer with such functionality.
dpimka