如何更改 QTabWidget 中的文本对齐方式?
我找不到在 QTabWidget 中设置文本对齐方式的方法。
创建此小部件的实例后,我将其 tabPosition 属性设置为 West,但我希望它水平显示文本/标签。 我已经查看了 Qt 的 样式表,但是正如您所见可以看到,text-align属性只能在QPushButton和QProgressBar上设置。
我已经在网上搜索过,但我刚刚找到了一个 bugreport,一个 未回答的问题,最后用户建议重新实现paint()方法。 也许我可以解决,但我正在使用 Python(PyQt 或 PySide),并且我不知道该怎么做。
你能帮助我吗?
编辑: 感谢 Teukamm,我写了一些代码:
from PyQt4 import QtGui, QtCore
class HorizontalTabWidget(QtGui.QTabBar):
def paintEvent(self, event):
for index in range(self.count()):
painter = QtGui.QPainter()
painter.begin(self)
painter.setPen(QtCore.Qt.blue);
painter.setFont(QtGui.QFont("Arial", 10));
tabRect = self.tabRect(index)
painter.drawText(tabRect, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(index));
painter.end()
def sizeHint(self):
return QtCore.QSize(60, 130)
import sys
app = QtGui.QApplication(sys.argv)
tabs = QtGui.QTabWidget()
tabs.setTabBar(HorizontalTabWidget())
widget1 = QtGui.QWidget()
widget2 = QtGui.QWidget()
tabs.addTab(widget1, "Widget1")
tabs.addTab(widget2, "Widget2")
tabs.setTabPosition(2)
tabs.show()
sys.exit(app.exec_())
最后,我的文本按照我的预期对齐,但我有一个小(大?)问题:当您单击每个选项卡按钮的右侧时,它不会t 发送 currentChanged 信号 。我还尝试在 PaintEvent 中扩展每个 tabRect 的宽度,但它不起作用。我应该改变什么?
谢谢:)
顺便说一句:你不能从 QTabWidget 继承,而是从 QTabBar 继承;)
编辑:
解决了!只需更改 tabSizeHint 中的方法 sizeHint 即可,效果很好:)
I cannot find a way to set the text alignment in a QTabWidget.
After I've created an instance of this widget, I've set its tabPosition property to West, but I wish it showed text/label horizontally.
I've given a look to the Qt's stylesheets, but as you can see, the text-align property can only be set on QPushButton and QProgressBar.
I already searched on the web, but I just found a bugreport, a non-answered question, and finally a user that suggests to re-implement the paint() method.
Maybe I'd solve, but I'm using Python (PyQt or PySide) and I don't know how to do it.
Can you help me?
EDIT:
thanks to Teukamm, I wrote a bit of code:
from PyQt4 import QtGui, QtCore
class HorizontalTabWidget(QtGui.QTabBar):
def paintEvent(self, event):
for index in range(self.count()):
painter = QtGui.QPainter()
painter.begin(self)
painter.setPen(QtCore.Qt.blue);
painter.setFont(QtGui.QFont("Arial", 10));
tabRect = self.tabRect(index)
painter.drawText(tabRect, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(index));
painter.end()
def sizeHint(self):
return QtCore.QSize(60, 130)
import sys
app = QtGui.QApplication(sys.argv)
tabs = QtGui.QTabWidget()
tabs.setTabBar(HorizontalTabWidget())
widget1 = QtGui.QWidget()
widget2 = QtGui.QWidget()
tabs.addTab(widget1, "Widget1")
tabs.addTab(widget2, "Widget2")
tabs.setTabPosition(2)
tabs.show()
sys.exit(app.exec_())
And finally I've my text aligned as I expected, but I've a little (big?) problem: when you click on the right side of every tab button, it doesn't send the currentChanged SIGNAL. I've also tried to expand the width of every tabRect, in paintEvent, but it doesn't work. What should I change?
Thank you :)
BTW: you could not inherit from QTabWidget, but from QTabBar ;)
EDIT:
Solved! Just changed the method sizeHint in tabSizeHint and it works well :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 GitHub 上整理了一个可以解决此问题的示例: https://gist.github.com /LegoStormtroopr/5075267
代码也被复制:
Minimal example.py:
FingerTabs.py:
I've put a worked example together on GitHub that solves this here: https://gist.github.com/LegoStormtroopr/5075267
The code is copied across as well:
Minimal example.py:
FingerTabs.py:
首先,您需要创建一个作为 QtGui/QTabWidget 子类的自定义类,并重新定义绘制方法:
这是 您正在重新实现的 QWidget.paintEvent 方法的文档。
当然,您需要了解绘画的一般工作原理,请参阅 QPainter。
不幸的是,我目前手头没有 PyQt 安装,所以我无法为您提供更具体的解决方案。
To get you started, you need to create a custom class that is a subclass of QtGui/QTabWidget and redefine the painting method:
Here's the documentation for QWidget.paintEvent method that you are reimplementing.
Of course you need to know how painting works in general, please refer to the documentation for QPainter.
Unfortunately I don't have a PyQt installation handy at the moment, so I can't give you a more specific solution.