我需要自定义我的QTabWidget,以便它的一个标签栏(可以说总共有4个标签)具有扩展属性,并将填充其他标签之间的剩余空间。有任何想法吗?
您可以QTabBar
将其子类化,将其设置为tabwidget的宽度(高度取决于fontsize)并覆盖tabSizeHint()
:
class tabBar(QTabBar):
def __init__(self, width, height, parent=None):
QTabBar.__init__(self, parent)
self.setFixedSize(width, height)
def tabSizeHint(self, i):
f = 3 # Tab3 shall be f times wider then the other tabs
tw = int(self.width()/(self.count() + f -1)) # width per Tab
if i == 2: # Tab3
# return QSize(tw*f, self.height()) edited -> rounding error possible
return QSize(self.width() - (self.count() - 1)*tw, self.height())
return QSize(tw, self.height()) # all other tabs
并将此tabBar设置为您的tabwidget:
tb = tabBar(tabWidget.width(), 34) # tabBars height depends on fontSize
tabwidget..setTabBar(tb)
看起来像这样:
编辑:
如果调整tabWidget的大小,resizeEvent()
则会发生。此时,tabWidget已经具有其新大小,并在resizeEvent()之后立即重新绘制,请参见QT-Doc QTabWidget.resizeEvent
因此,如果将width()
tabBar的改编为resizeEvent()
,则tabBar的宽度将始终与tabwidget相同。因为tabSizeHint()
取决于宽度,所以所有选项卡也将具有正确的宽度。因此,您可以继承QTabWidget()
并覆盖resizeEvent()
动态解决方案:
class tabWidget(QTabWidget):
def __init__(self, parent=None):
QTabWidget.__init__(self, parent)
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
QTabWidget.resizeEvent(self, event)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句