有没有办法让 QTabWidgets 在只有一个选项卡可见(但有多个选项卡)的情况下自动隐藏选项卡栏?

问题描述 投票:0回答:1

我有一个名为 QTabWidget

tabWidget
 实例,它有两个 
QTableWidget
子级。

我在 setTabBarAutoHide(true)

 上调用了 
QTabWidget
,并且还使用 setTabVisible
 作为第二个选项卡的参数来调用 
false

我预计

setTabBarAutoHide(true)
意味着不会显示选项卡栏,因为只有一个可见选项卡。然而,情况并非如此 - 只有一个可见的选项卡,并且该栏未隐藏。

如果我从不添加第二个选项卡,则行为符合预期 - 选项卡栏保持隐藏状态。

这是我的代码的粗略想法:

QTabWidget* tabWidget = new QTabWidget();
tabWidget->setTabBarAutoHide(true);

QTableWidget* firstTab = new QTableWidget();
tabWidget->addTab(firstTab, "First Tab");

QTableWidget* secondTab = new QTableWidget();
tabWidget->addTab(secondTab, "Second Tab");
tabWidget->setTabVisible(1, false);

如果只有一个选项卡可见(但存在多个选项卡),是否有一种“好”的方法来自动隐藏选项卡栏?如果没有,获得此功能的最惯用的方法是什么?

c++ qt qtabwidget
1个回答
0
投票

在 Qt 中,当只有

一个选项卡可见时,
setTabBarAutoHide(true) 确实应该自动隐藏选项卡栏。但是,使用 setTabVisible(false) 将选项卡设置为不可见似乎可能存在冲突

要实现您想要的行为,您可以使用解决方法。您可以将其从 QTabWidget 中删除,而不是使用 setTabVisible(false) 来隐藏第二个选项卡。然后,当您想再次显示它时,可以将其添加回来。这样,setTabBarAutoHide(true) 应该按预期工作。

像这样:

#include <QApplication> #include <QTabWidget> #include <QTableWidget> int main(int argc, char *argv[]) { QApplication a(argc, argv); // Create the QTabWidget QTabWidget* tabWidget = new QTabWidget(); tabWidget->setTabBarAutoHide(true); // Create the first tab and add it to the tab widget QTableWidget* firstTab = new QTableWidget(); tabWidget->addTab(firstTab, "First Tab"); // Create the second tab and add it to the tab widget QTableWidget* secondTab = new QTableWidget(); tabWidget->addTab(secondTab, "Second Tab"); // Remove the second tab from the tab widget tabWidget->removeTab(1); // Show the tab widget tabWidget->show(); return a.exec(); }
    
© www.soinside.com 2019 - 2024. All rights reserved.