pyqt qtabwidget horizontal tab and horizontal text in QtDesigner

前端 未结 4 500
半阙折子戏
半阙折子戏 2020-12-21 12:37

i am having problem to change text alignment using pyqt4 desginer i have made tabs horizontal by aligning west but the text in that goes north to south that looks bad i want

4条回答
  •  醉梦人生
    2020-12-21 13:21

    I know this has been awhile but if anyone needs @Sahil Jain's answer in PyQt5 version, please refer to following for your tabwidget.py

    from PyQt5 import QtGui, QtCore, QtWidgets
    
    
    class HorizontalTabBar(QtWidgets.QTabBar):
        def paintEvent(self, event):
    
            painter = QtWidgets.QStylePainter(self)
            option = QtWidgets.QStyleOptionTab()
            for index in range(self.count()):
                self.initStyleOption(option, index)
                painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, option)
                painter.drawText(self.tabRect(index),
                                 QtCore.Qt.AlignCenter | QtCore.Qt.TextDontClip,
                                 self.tabText(index))
    
        def tabSizeHint(self, index):
            size = QtWidgets.QTabBar.tabSizeHint(self, index)
            if size.width() < size.height():
                size.transpose()
            return size
    
    
    class TabWidget(QtWidgets.QTabWidget):
        def __init__(self, parent=None):
            QtWidgets.QTabWidget.__init__(self, parent)
            self.setTabBar(HorizontalTabBar())
    

提交回复
热议问题