PyQt5如何设置TabWidget向西但保持文本水平
PyQt5 How To Set TabWidget West But Keep The Text Horizontal
如何使文本方向从左到右而不是从上到下
在这个回答中,我将对我用 C++ 编写的 进行 python 翻译。
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)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = TabWidget()
w.addTab(QtWidgets.QWidget(), "tab1")
w.addTab(QtWidgets.QWidget(), "tab2")
w.addTab(QtWidgets.QWidget(), "tab3")
w.show()
sys.exit(app.exec_())
回答了 PyQt4 版本,但我也上传了 PyQt5 版本。
请按照流程推广TabWidget
如何使文本方向从左到右而不是从上到下
在这个回答中,我将对我用 C++ 编写的
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)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = TabWidget()
w.addTab(QtWidgets.QWidget(), "tab1")
w.addTab(QtWidgets.QWidget(), "tab2")
w.addTab(QtWidgets.QWidget(), "tab3")
w.show()
sys.exit(app.exec_())
回答了