[英]QTabWidget Hide and Show tabs
I have some problems with QTabWidget.我在使用 QTabWidget 时遇到了一些问题。 In case of the missing Hide functionality I have to build my own.如果缺少隐藏功能,我必须自己构建。 According to the documentation I use removeTab and insertTab, but with insert Tab I have a problem to show the Tab page that is removed.根据文档,我使用 removeTab 和 insertTab,但使用插入选项卡时,我无法显示已删除的选项卡页面。
I use to add我用来添加
RibbonTabContent *ribbonTabContent = new RibbonTabContent;
QTabWidget::addTab(ribbonTabContent, tabIcon, tabName);
To remove is use:删除是使用:
void Ribbon::hideTab(const QString &tabName)
{
// Find ribbon tab
for (int i = 0; i < count(); i++)
{
if (tabText(i).toLower() == tabName.toLower())
{
QTabWidget::removeTab(i);
break;
}
}
}
Both functions are working, pWidget is always null.两个函数都在工作,pWidget 始终为空。 But now the insert function do not work well.但是现在插入功能不能正常工作。 I think there I have a problem, but do not understand my problem.我认为我有问题,但不明白我的问题。
void Ribbon::showTab(const QString &tabName){
// Find ribbon tab
QWidget* pWidget= QTabWidget::findChild<RibbonTabContent *>(tabName);
if(pWidget){
QTabWidget::insertTab(2,pWidget, tabName);
}
}
Maybe someone can help me out?也许有人可以帮助我?
If you call QTabWidget::removeTab
you remove the tab at the specified index from the children tree of your QTabWidget
, the tab instance is not actually deleted though, so when you search for that same tab with QTabWidget::findChild
you can't find it because it's not a child of your QTabWidget
anymore.如果调用QTabWidget::removeTab
从您的孩子树删除指定索引处的标签QTabWidget
,标签实例,实际上不会删除的,所以当你搜索具有同一标签QTabWidget::findChild
你不能找到因为它不再是你的QTabWidget
的孩子了。 From the code you show I think you probably would not find it anyway since findChild
searches for a widget with the specified objectName
but you never set it for your tab.从您显示的代码中,我认为您可能无论如何都找不到它,因为findChild
搜索具有指定objectName
的小部件,但您从未为您的选项卡设置它。
A solution would be to store the removed tabs and then restore them when you please.一个解决方案是存储删除的选项卡,然后在需要时恢复它们。
Assuming m_hiddenTabs
is a QHash<QString, QWidget*>
or QMap<QString, QWidget*>
you could try something like this.假设m_hiddenTabs
是QHash<QString, QWidget*>
或QMap<QString, QWidget*>
你可以尝试这样的事情。
void Ribbon::hideTab(const QString &tabName)
{
// Find ribbon tab
for (int i = 0; i < count(); i++)
{
if (tabText(i).toLower() == tabName.toLower())
{
m_hiddenTabs.insert(tabName.toLower(), QTabWidget::widget(i));
QTabWidget::removeTab(i);
break;
}
}
}
void Ribbon::showTab(const QString &tabName){
// Find ribbon tab
auto tab = m_hiddenTabs.take(tabName.toLower());
if(tab){
QTabWidget::insertTab(2, tab, tabName);
}
}
Since Qt 5.15 it is also possible to use setTabVisible
:从 Qt 5.15 开始,也可以使用setTabVisible
:
void QTabWidget::setTabVisible(int index, bool visible)
If visible is true, the page at position index is visible;如果visible 为true,则位置index 处的页面可见; otherwise the page at position index is hidden.否则位置索引处的页面将被隐藏。 The page's tab is redrawn appropriately.If visible is true, the page at position index is visible;页面的标签被适当地重绘。如果visible 为true,则位置index 处的页面可见; otherwise the page at position index is hidden.否则位置索引处的页面将被隐藏。 The page's tab is redrawn appropriately.页面的选项卡被适当地重绘。
It is unfortunate that QTabBar is unable to 'hide' a tab.不幸的是 QTabBar 无法“隐藏”选项卡。
Here is my very easy work-around: mark the tabs 'disabled' instead (eg ui->tabWidget->setTabEnabled(tabIndex, false);
).这是我非常简单的解决方法:将选项卡标记为“已禁用”(例如ui->tabWidget->setTabEnabled(tabIndex, false);
)。
Then, use stylesheets to style the "disabled" tab as entirely invisible and taking up no space:然后,使用样式表将“禁用”选项卡设置为完全不可见且不占用空间:
QTabBar::tab:disabled
{
min-width: 0px;
max-width: 0px;
color: rgba(0,0,0,0);
background-color: rgba(0,0,0,0);
}
This works near-perfectly for me, with the only downside being that you can't have both disabled and "hidden" tabs in the same tabbar.这对我来说几乎完美,唯一的缺点是你不能在同一个标签栏中同时使用禁用和“隐藏”标签。 However, usually I want one or the other, not both in the same bar.但是,通常我想要一个或另一个,而不是在同一个酒吧里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.