Qt - QTable 可以将列标签旋转 90 度吗?
我有许多带有很长标签的狭窄列。我想将标签旋转 90 度。是否可以?
I have many narrow columns with very long labels. I want to rotate the labels by 90 degrees. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能必须继承 QTableWidgetItem 并实现您自己的垂直文本绘制。然后在表格上使用
setHorizontalHeaderItem()
来指向新小部件的实例。You will probably have to subclass QTableWidgetItem and implement your own vertical text painting. Then use
setHorizontalHeaderItem()
on your table to point to an instance of your new widget.在寻找这个问题的答案时,我发现了很多提示,但没有真正的答案。提示告诉我们子类化 QHeaderView 并重新实现 PaintSection。当我尝试在 PyQt4 中执行此操作并尝试按照 QHeaderView 的源代码从头开始实现 PaintSection 时,我没有成功。然而,简单地旋转画家实例并调整所有尺寸提示就成功了。
该代码仅适用于水平标题,并且非常紧凑:
When searching for an answer to this questions I found many hints, but no real answer. The hints tell one to subclass QHeaderView and re-implement paintSection. When I tried to do so in PyQt4 and tried to implement paintSection from scratch, following the source of QHeaderView, I was not successful. However, simply rotating the painter instance and adjusting all the size hints was successful.
The code works for horizontal headers only and is nicely compact:
我制作了一个自定义脚本,根据之前的答案可以正常工作。
将下一个代码复制并粘贴到rotated.py 文件中,
然后使用此行从 main.py 文件导入此类:
并使用此行完成操作:
希望值得!
I've made a custom script that works fine based on previous answer..
copy and paste the next code in a rotated.py file
then import from your main.py file this class using this line:
and complete the actions with this line:
hope worth it!