pyqt qtabwidget horizontal tab and horizontal text in QtDesigner

前端 未结 4 501
半阙折子戏
半阙折子戏 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())
    
    0 讨论(0)
  • 2020-12-21 13:27

    Using the above method and after that adding a line of code to it and got my icons displayed

    from PyQt4 import QtGui, QtCore
    
    
    class HorizontalTabBar(QtGui.QTabBar):
        def paintEvent(self, event):
            painter = QtGui.QStylePainter(self)
            option = QtGui.QStyleOptionTab()
            for index in range(self.count()):
                self.initStyleOption(option, index)
                painter.drawControl(QtGui.QStyle.CE_TabBarTabShape, option)
                painter.drawText(self.tabRect(index),
                                 QtCore.Qt.AlignCenter | QtCore.Qt.TextDontClip,
                                 self.tabText(index))
                if index == 0:
                    painter.drawImage(QtCore.QRectF(10, 10, 66, 67), QtGui.QImage("ico/HOME.png"))
    
        def tabSizeHint(self, index):
            size = QtGui.QTabBar.tabSizeHint(self, index)
            size.setHeight=50
            size.setWidth=200
            if size.width() < size.height():
                size.transpose()
            return size
    
    
    class TabWidget(QtGui.QTabWidget):
        def __init__(self, parent=None):
            QtGui.QTabWidget.__init__(self, parent)
            self.setTabBar(HorizontalTabBar())
    

    you can add icons as per the indexes of your tabs and set their position accordingly till now this is best solution i can give.cheers

    0 讨论(0)
  • 2020-12-21 13:30

    The solution that I propose may not be the exact solution but I think it is the one that comes closest. What I propose is to promote the QTabWidget to use a custom QTabWidget.

    Before that I have improved the solution proposed in this answer:

    tabwidget.py

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

    This file will be stored next to the .ui file and the .py files as shown in the following structure:

    .
    ├── main.py        # file of class MainWindow(QMainWindow,Ui_MainWindow):
    ├── tabwidget.py   # custom QTabWidget
    ├── untitled.py    
    └── untitled.ui    # your design
    

    After having the previous structure we open the .ui file with Qt Designer and we right click on the QTabWidget and select promoted to ...:

    A dialogue will open and the following should be placed in it:

    Then press the add button and then the promote button, and at the end you generate the .py file again with the help of pyuic

    At the end you get the following widget:

    0 讨论(0)
  • 2020-12-21 13:36

    Better way for looks and usability:

    At the Qt Designer>

    1. Use QToolBox at left side and QTabWidget at right.
    2. Connect both with signal (currentChanged(int) - setCurrentIndex(int)).
    3. Hide QTabWidget header with following css.

    QTabBar::tab {height: 0px;}

    Here an Example:

    0 讨论(0)
提交回复
热议问题