列自动加强QTableView的大小
我刚刚设法使 qtableView
与我的模型一起使用。它已固定3列。当我打开一个窗口时,看起来还可以,但是当我调整窗口大小时, qtableView
本身会大小,但是列的宽度保持不变。
我希望列调整大小以适合 qtableView
每次调整窗口大小时。
有什么内置的方法可以使其起作用吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此代码同样伸展每列,以便它们适合表的宽度。
docs:
This code equally stretches each column so that they fit the table's width.
Docs:
有一个标头标志,以确保QTableView的最后一列如果调整大小,则填充其父。您可以这样设置它:
但是,这不会按比例调整其他列的大小。如果您也想这样做,则可以在父母的resizeevent中处理它:
qparent类是qmainwindow的子类。
There is a header flag to ensure that the QTableView's last column fills up its parent if resized. You can set it like so:
However, that does not resize the other columns proportionately. If you want to do that as well, you could handle it inside the resizeEvent of your parent thusly:
QParent class is subclass of QMainWindow.
小部件 qtableview , qtableWidget )具有这两种有用的方法:
如果您打开了类的文档 qheaderview ,您将找到用于设置“项目视图”行或列标头的外观和行为的方法。您可以通过以下一种方法之一解决问题:
AS Davy Jones 提到。
示例:
作为模式您可以设置qheaderview :: strenth或qheaderview :: resizetocontents。
不幸的是,此方法有一个缺点 - 应用后您将无法手动(以GUI)或编程方式更改列(或行)的大小。
示例:
Widgets QTableView, QTreeView and their derived classes (such as QTableWidget) have this two usefull methods:
If you open documentation for a class QHeaderView, you will find methods that set up appearance and behavior of row or column header for item views. You can resolve your problem by one of these methods:
void QHeaderView::stretchLastSection( bool stretch )
As Davy Jones mentioned.
Example:
void QHeaderView::setResizeMode( ResizeMode mode )
As mode you can set QHeaderView::Stretch or QHeaderView::ResizeToContents.
Unfortunately this method have a drawback - after it's apply you will not be able to change size of columns (or rows) manually (in GUI) or programmatically.
Example:
在pyqt5中,您可以通过以下方式在table_widget中实现此目标:
In PyQt5 you can achieve this in your table_widget by doing: