更改 QTabWidget 中的小部件

CTZ*_*tef 4 qt

我找不到在运行时替换,QWidget中特定选项卡的方法QTabWidget。我可以使用addTabthen setCurrentWidget,但是如果我只想在选项卡索引 1 处设置 aQWidget而不添加新选项卡怎么办?

Mat*_*hew 6

一种方法是使用父项QWidget作为选项卡中的主项QWidget,并在您想要更改选项卡内容时更改其子项。

从技术上讲,您不会更改QWidget选项卡本身,而只是更改选项卡中主选项卡的第一个子选项QWidget

例如:

QTabWidget* myTabWidget = new QTabWidget();
QWidget* tab1 = new QWidget();
QWidget* tab1Contents = new QWidget( tab1 );

// [1] Setup the first tab.
myTabWidget.addTab( tab1, "1st Tab" );

// [3] If you now want to remove the original contents 
// and replace with something new.
delete tab1Contents;
QWidget* tab1NewContents = new QWidget( tab1 );
Run Code Online (Sandbox Code Playgroud)

当然,您可以向tab1tab1Contents、 中添加布局,并tab1NewContents确保您的选项卡内容看起来不错!