gpt4 book ai didi

python - pyqt5 tabwidget 垂直制表符水平文本左对齐

转载 作者:太空宇宙 更新时间:2023-11-03 15:43:25 25 4
gpt4 key购买 nike

因为 pyqt 在垂直选项卡选项中没有水平文本,所以我遵循了这个 link让它发生。我想在左侧有图标,然后在图标后显示文本,并为选定的选项卡文本、非事件选项卡文本设置不同的颜色。下面的代码几乎完成了。唯一的问题是文本对齐居中。我尝试更改 tabRect.center() 但将其更改为 left 和 top 或 right 等使其崩溃。

我从this link得到的注释代码让我左对齐,但它没有我添加的图标。但是我无法更改非事件标签的文本颜色。

我是 python 的新手,我找不到解决方案。我也试过了link但这只会设置背景颜色。也尝试使用此选项 setTabTextColor link但由于某种原因它没有用。从 2 天开始一​​直在尝试。

每当我尝试使用带有注释代码的样式表设置文本颜色时,“颜色”选项在样式表中不起作用。关于如何完成这项工作的任何想法?谢谢

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)

最佳答案

解决方案是使用QProxyStyle 来重定向文本绘制:

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_())

enter image description here

关于python - pyqt5 tabwidget 垂直制表符水平文本左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51404102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com