<small id='joN84'></small><noframes id='joN84'>

      <bdo id='joN84'></bdo><ul id='joN84'></ul>
  1. <tfoot id='joN84'></tfoot>
    <legend id='joN84'><style id='joN84'><dir id='joN84'><q id='joN84'></q></dir></style></legend>

    <i id='joN84'><tr id='joN84'><dt id='joN84'><q id='joN84'><span id='joN84'><b id='joN84'><form id='joN84'><ins id='joN84'></ins><ul id='joN84'></ul><sub id='joN84'></sub></form><legend id='joN84'></legend><bdo id='joN84'><pre id='joN84'><center id='joN84'></center></pre></bdo></b><th id='joN84'></th></span></q></dt></tr></i><div id='joN84'><tfoot id='joN84'></tfoot><dl id='joN84'><fieldset id='joN84'></fieldset></dl></div>

    1. pyqt5 tabwidget 垂直制表符 水平文本左对齐

      时间:2023-08-05
      <i id='0jQ8Q'><tr id='0jQ8Q'><dt id='0jQ8Q'><q id='0jQ8Q'><span id='0jQ8Q'><b id='0jQ8Q'><form id='0jQ8Q'><ins id='0jQ8Q'></ins><ul id='0jQ8Q'></ul><sub id='0jQ8Q'></sub></form><legend id='0jQ8Q'></legend><bdo id='0jQ8Q'><pre id='0jQ8Q'><center id='0jQ8Q'></center></pre></bdo></b><th id='0jQ8Q'></th></span></q></dt></tr></i><div id='0jQ8Q'><tfoot id='0jQ8Q'></tfoot><dl id='0jQ8Q'><fieldset id='0jQ8Q'></fieldset></dl></div>
        <tbody id='0jQ8Q'></tbody>
      • <small id='0jQ8Q'></small><noframes id='0jQ8Q'>

        <tfoot id='0jQ8Q'></tfoot>
          <bdo id='0jQ8Q'></bdo><ul id='0jQ8Q'></ul>

              <legend id='0jQ8Q'><style id='0jQ8Q'><dir id='0jQ8Q'><q id='0jQ8Q'></q></dir></style></legend>

                本文介绍了pyqt5 tabwidget 垂直制表符 水平文本左对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                由于 pyqt 在垂直选项卡选项中没有水平文本,我遵循了这个

                Since pyqt doesn't have horizontal text in vertical tab option, I followed this link to make it happen. I wanted to have icons on the left and then text after icon and different color for selected tab text, inactive tabs text. Below code gets it done almost. The only problem is text alignment is center. I tried changing tabRect.center() but changing it with left and top or right etc is making it crash.

                The commented code which I got from this linkgets me left alignment but it didn't have icons which I added. But in that I am unable to change text color of inactive tabs.

                I am new to python and I am unable to find a solution for this. I tried this as well link but this only sets background color. tried using this option as well setTabTextColor link but it didn't work for some reason. been trying from 2 days.

                Whenever I try to set text color using stylesheet with commented code, the "color" option won't work in stylesheet. any ideas on how to get this done? thanks

                from PyQt5 import QtCore, QtGui, QtWidgets
                
                class TabBar(QtWidgets.QTabBar):
                    def tabSizeHint(self, index):
                        s = QtWidgets.QTabBar.tabSizeHint(self, index)
                        s.transpose()
                        return s
                
                    def paintEvent(self, event):
                        painter = QtWidgets.QStylePainter(self)
                        opt = QtWidgets.QStyleOptionTab()
                
                        for i in range(self.count()):
                            self.initStyleOption(opt, i)
                            painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, opt)
                            painter.save()
                
                            s = opt.rect.size()
                            s.transpose()
                            r = QtCore.QRect(QtCore.QPoint(), s)
                            r.moveCenter(opt.rect.center())
                            opt.rect = r
                
                            c = self.tabRect(i).center()
                            painter.translate(c)
                            painter.rotate(90)
                            painter.translate(-c)
                            painter.drawControl(QtWidgets.QStyle.CE_TabBarTabLabel, opt)
                            painter.restore()
                
                        # for i in range(self.count()):
                        #     self.initStyleOption(opt, i)
                        #     c = self.tabRect(i)
                        #     c.moveLeft(35)
                        #     painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, opt)
                        #     # painter.setPen(QColor(255, 255, 255))
                        #     painter.drawText(c, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(i))
                        #     if i == 0:
                        #         painter.drawImage(QtCore.QRectF(8, 8, 20, 20), QtGui.QImage("images/logo.png"))
                        #     if i == 1:
                        #         painter.drawImage(QtCore.QRectF(8, 44, 20, 20), QtGui.QImage("images/data.png"))
                        #     if i == 2:
                        #         painter.drawImage(QtCore.QRectF(8, 82, 20, 20), QtGui.QImage("images/browse.png"))
                        #     if i == 3:
                        #         painter.drawImage(QtCore.QRectF(8, 120, 20, 20), QtGui.QImage("images/off.png"))
                        #     if i == 4:
                        #         painter.drawImage(QtCore.QRectF(8, 158, 20, 20), QtGui.QImage("images/cal.png"))
                        #     if i == 5:
                        #         painter.drawImage(QtCore.QRectF(8, 196, 20, 20), QtGui.QImage("images/fol.png"))
                        #     if i == 6:
                        #         painter.drawImage(QtCore.QRectF(8, 232, 20, 20), QtGui.QImage("images/exc.png"))
                        # painter.end()
                
                
                class TabWidget(QtWidgets.QTabWidget):
                    def __init__(self, *args, **kwargs):
                        QtWidgets.QTabWidget.__init__(self, *args, **kwargs)
                        self.setTabBar(TabBar(self))
                        self.setTabPosition(QtWidgets.QTabWidget.West)
                

                解决方案

                The solution is to use a QProxyStyle to redirect text painting:

                from PyQt5 import QtCore, QtGui, QtWidgets
                
                
                class TabBar(QtWidgets.QTabBar):
                    def tabSizeHint(self, index):
                        s = QtWidgets.QTabBar.tabSizeHint(self, index)
                        s.transpose()
                        return s
                
                    def paintEvent(self, event):
                        painter = QtWidgets.QStylePainter(self)
                        opt = QtWidgets.QStyleOptionTab()
                
                        for i in range(self.count()):
                            self.initStyleOption(opt, i)
                            painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, opt)
                            painter.save()
                
                            s = opt.rect.size()
                            s.transpose()
                            r = QtCore.QRect(QtCore.QPoint(), s)
                            r.moveCenter(opt.rect.center())
                            opt.rect = r
                
                            c = self.tabRect(i).center()
                            painter.translate(c)
                            painter.rotate(90)
                            painter.translate(-c)
                            painter.drawControl(QtWidgets.QStyle.CE_TabBarTabLabel, opt);
                            painter.restore()
                
                
                class TabWidget(QtWidgets.QTabWidget):
                    def __init__(self, *args, **kwargs):
                        QtWidgets.QTabWidget.__init__(self, *args, **kwargs)
                        self.setTabBar(TabBar(self))
                        self.setTabPosition(QtWidgets.QTabWidget.West)
                
                class ProxyStyle(QtWidgets.QProxyStyle):
                    def drawControl(self, element, opt, painter, widget):
                        if element == QtWidgets.QStyle.CE_TabBarTabLabel:
                            ic = self.pixelMetric(QtWidgets.QStyle.PM_TabBarIconSize)
                            r = QtCore.QRect(opt.rect)
                            w =  0 if opt.icon.isNull() else opt.rect.width() + self.pixelMetric(QtWidgets.QStyle.PM_TabBarIconSize)
                            r.setHeight(opt.fontMetrics.width(opt.text) + w)
                            r.moveBottom(opt.rect.bottom())
                            opt.rect = r
                        QtWidgets.QProxyStyle.drawControl(self, element, opt, painter, widget)
                
                if __name__ == '__main__':
                    import sys
                
                    app = QtWidgets.QApplication(sys.argv)
                    QtWidgets.QApplication.setStyle(ProxyStyle())
                    w = TabWidget()
                    w.addTab(QtWidgets.QWidget(), QtGui.QIcon("zoom.png"), "ABC")
                    w.addTab(QtWidgets.QWidget(), QtGui.QIcon("zoom-in.png"), "ABCDEFGH")
                    w.addTab(QtWidgets.QWidget(), QtGui.QIcon("zoom-out.png"), "XYZ")
                
                    w.resize(640, 480)
                    w.show()
                
                    sys.exit(app.exec_())
                

                这篇关于pyqt5 tabwidget 垂直制表符 水平文本左对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:将 numpy 列表数组转换为 numpy 数组 下一篇:检测 Widget-window resized 信号中的调整大小

                相关文章

                最新文章

                  <i id='ibm1c'><tr id='ibm1c'><dt id='ibm1c'><q id='ibm1c'><span id='ibm1c'><b id='ibm1c'><form id='ibm1c'><ins id='ibm1c'></ins><ul id='ibm1c'></ul><sub id='ibm1c'></sub></form><legend id='ibm1c'></legend><bdo id='ibm1c'><pre id='ibm1c'><center id='ibm1c'></center></pre></bdo></b><th id='ibm1c'></th></span></q></dt></tr></i><div id='ibm1c'><tfoot id='ibm1c'></tfoot><dl id='ibm1c'><fieldset id='ibm1c'></fieldset></dl></div>
                  <legend id='ibm1c'><style id='ibm1c'><dir id='ibm1c'><q id='ibm1c'></q></dir></style></legend>
                    <bdo id='ibm1c'></bdo><ul id='ibm1c'></ul>

                  1. <small id='ibm1c'></small><noframes id='ibm1c'>

                  2. <tfoot id='ibm1c'></tfoot>