Question

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?

Was it helpful?

Solution

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().

OTHER TIPS

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" :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top