QTableView 放大/缩小 [英] QTableView Zoom In/Out

查看:98
本文介绍了QTableView 放大/缩小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以像在 Excel 中一样放大和缩小的 QTableView.
这里问了一个类似的问题:QWidget 上的缩放功能

I'm trying to create a QTableView that can be zoomed in and out like in Excel.
A similar question was asked here: Zooming function on a QWidget

但是,我在 PyQt 而非 C 中继承 QTableView,因此重新实现整个 PaintEvent 方法有点进化.其源代码有点复杂:https://qt.gitorious.org/qt/tiittane-qt/source/bdd4a9149789f60974603e1f7621d51378f0a108:src/gui/itemviews/qtableview.cpp#L1282

However, I'm subclassing the QTableView in PyQt and not C so reimplementing the entire PaintEvent method is a bit evolved. The source code for that is a bit complex: https://qt.gitorious.org/qt/tiittane-qt/source/bdd4a9149789f60974603e1f7621d51378f0a108:src/gui/itemviews/qtableview.cpp#L1282

我想看看是否还有其他可行的选项来拥有可缩放的 TableView.我的第一次尝试是通过设置字体大小然后意识到每一列和行的宽度也必须缩放,这可能会变慢.然后意识到改变字体会改变打印.这似乎不是一个优雅的解决方案.在绘画之前改变画家的比例似乎是更优雅的解决方案,但必须重新实现并将相当多的代码转换为 python 才能这样做.我想知道是否还有其他钩子可以完成这项工作.

I'm looking to see if there are any other viable options to have a zoom able TableView. My first attempt was by setting the font size then realized each column and row widths would have to scale as well which can become slow. Then realized changing the font would change the print. It didn't seems like an elegant solution. Changing the scale of the painter before painting seems like the more elegant solution but would have to re-implement and translate quite a bit of code to python to do so. I'm wondering if there are any other hooks to get this done.

谢谢

推荐答案

如果您可以使用 QTableWidget 代替,那么您可以创建一个 QGraphicsScene将其添加到其中.然后您可以轻松控制小部件的比例.

If you can use QTableWidget instead then you can create a QGraphicsScene and add it to that. Then you easily control the scale of the widget within.

如果您希望垂直和水平标题始终可见,我认为您将不得不关闭表格的滚动条(最终会放大,无论如何可能不是您想要的)并让滚动条成为包含图形场景的面板的一部分(可能面板是一个 QAbstractScrollArea,布局中有 4 个单元格:一个单元格用于场景,一个用于水平滚动条,一个用于垂直滚动条,一个用于可能为空的角落),并将它们连接到表格的滚动行为.

If you want vertical and horizontal headers always visible I think you will have to turn off the table's scrollbars (which would end up zoomed, probably not what you want anyways) and have the scrollbars part of the panel that contains the graphics scene (probably panel would be a QAbstractScrollArea with 4 cells in layout: one cell for scene, one for horiz scrollbar, one for vert scrollbar, and one for the corner maybe empty), and connect them to the table's scroll behavior.

这篇关于QTableView 放大/缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆