I am new to Qt and I have just managed to make a QTableView work with my model. It has fixed 3 columns. When I open a window, it look ok but when i resize the window, the QTableView itself gets resized but columns' width remains the same. Is there any build-in way to make it work? I want columns to resize to fit the edges of QTableView every the the window gets resized.
我是Qt的新手,我刚刚成功地让QTableView与我的模型一起工作。它有固定的三根柱子。当我打开一个窗口时,它看起来不错,但当我调整窗口大小时,QTableView本身会调整大小,但列的宽度保持不变。有没有什么内置的方法可以让它工作?我希望每次调整窗口大小时,列的大小都要调整到适合QTableView的边缘。
更多回答
Do you always want the column widths to be the same? If not, you can stretch the last column's width by calling table->horizontalHeader()->setStretchLastSection(true);
是否始终希望列宽相同?如果不是,您可以通过调用table->horizontalHeader()->setStretchLastSection(true);来拉伸最后一列的宽度
This code equally stretches each column so that they fit the table's width.
该代码均匀地拉伸每一列,使它们适合表格的宽度。
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
Docs:
文档:
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:
有一个标题标志,以确保QTableView的最后一列在调整大小时填充其父级。您可以这样设置它:
table_view->horizontalHeader()->setStretchLastSection(true);
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:
但是,这不会按比例调整其他列的大小。如果您也想这样做,您可以在父级的realzeEvent中处理它,如下所示:
void QParent::resizeEvent(QResizeEvent *event) {
table_view->setColumnWidth(0, this->width()/3);
table_view->setColumnWidth(1, this->width()/3);
table_view->setColumnWidth(2, this->width()/3);
QMainWindow::resizeEvent(event);
}
QParent class is subclass of QMainWindow.
QParent类是QMainWindow的子类。
Widgets QTableView, QTreeView and their derived classes (such as QTableWidget) have this two usefull methods:
小部件QTableView、QTreeView及其派生类(如QTableWidget)有这两个有用的方法:
QHeaderView* horizontalHeader() const;
QHeaderView* verticalHeader() const;
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:
如果打开类QHeaderView的文档,您将发现设置项视图的行或列标题的外观和行为的方法。您可以通过以下方法之一来解决问题:
void QHeaderView::stretchLastSection( bool stretch )
As Davy Jones mentioned.
Example:
QTableView *table = new QTableView();
table->horizontalHeader()->setStretchLastSection(true);
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:
QTableView *table = new QTableView();
table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
In PyQt5 you can achieve this in your table_widget by doing:
在PyQt5中,您可以通过执行以下操作在TABLE_Widget中实现这一点:
header = table_widget.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
更多回答
If you are using PyQt6, it becomes: header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.ResizeToContents)
如果您使用的是PYQt6,它将变为:header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.ResizeToContents)
我是一名优秀的程序员,十分优秀!