QTabWidget - 标签图标不在中心

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

我有一个带有六个选项卡的QTabWidget,所有选项卡都有一个图标 - 但图标不在选项卡的中心:

到目前为止我做了什么:

tabWidget->setStyleSheet("QTabBar::tab {width: 40px; height: 40px;}"
                        "QTabBar::tab:selected {background: lightblue;}");
tabWidget->setIconSize(QSize(40, 40));
tabWidget->addTab("widget", QIcon("iconPath"), ""); //<--for all six tabs

和:

tabWidget->setTabIcon(index, QIcon("iconPath"));

任何想法为什么会发生这种情况,以及我如何解决它?

qt tabs widget icons tabwidget
2个回答
4
投票

我也一直在努力解决这个问题。这是我如何解决它。

背景:

我试图得到一个左侧标签菜单,它使用图标作为其指标(用户会看到什么),但我有一个问题:

enter image description here

我在“属性编辑器”中使用currentTabIcon设置的图标与底部对齐(由于我使用的是West方向,因此预计会出现这种情况。通常情况下,将选择North方向,图标将位于左侧)。

我把它作为我的样式表:

QTabBar::tab:hover {
	background-color: #212121;
}

QTabBar::tab:selected{
    background-color: #313131;
}
QTabBar::tab {
    background-color: #111111;	
	height:70px;
	width: 70px;
	border: none;
}

现在,尝试在这篇文章中找到的建议的solution,我设置边距没有达到预期的效果,事实上它根本没有效果。

解:

在玩了一些CSS属性之后,我发现设置padding-toppadding-bottom给了我想要的结果。

enter image description here

添加行:

	padding-top: -15px;
	padding-bottom: 15px

解决了我的问题,但需要根据您的需要进行更改。

我的最终样式表类似于:

QTabBar::tab:hover {
	background-color: #212121;
}

QTabBar::tab:selected{
    background-color: #313131;
}
QTabBar::tab {
    background-color: #111111;	
	height:70px;
	width: 70px;
	border: none;
	margin: 0px;
	padding-top: -15px;
	padding-bottom: 15px
}

0
投票

如果有人和我一样的问题与标签中的图标有关,我会在几天和几天之后找到一个解决方案来搜索它,它很简单:D

只需将其添加到TabWidget的样式表:

tabWidget->setStyleSheet("::tab {margin: 0px;}");
                                 ************
© www.soinside.com 2019 - 2024. All rights reserved.