1

在我的 QT 应用程序中,我使用 QTabWidget 作为基本导航。我在 ui 中设置的这个 QTabWidget。在 QTabWidget 的某些选项卡中,我需要 QStackedWidget 才能“在视图中向下钻取”。

我也尝试在 ui 中添加 QStackedWidget,但它会自动将页面添加到堆栈中。我想在代码中添加 QStackedWidget 的页面。如果我在代码中尝试这样做,stackedWidget 已经有一个标准页面,所以 myWidget 将是堆栈中的第二个。

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

在 QTabWidget 选项卡中设置 QStackedWidget 的最佳和最简单的方法是什么?

4

2 回答 2

1

怎么样:

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

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

您还可以在 Qt 的 Designer/Creator 中删除所有现有的堆栈页面。只需右键单击堆叠的小部件并删除所有现有页面。然后您可以使用addWidget().

于 2010-01-26T09:57:19.983 回答
0

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

于 2010-01-26T11:37:38.980 回答