C++ 列自动调整为 QTableView 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18293403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Columns auto-resize to size of QTableView
提问by khajvah
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 与我的模型一起工作。它有固定的 3 列。当我打开一个窗口时,它看起来不错,但是当我调整窗口大小时,QTableView 本身会调整大小,但列的宽度保持不变。有什么内置的方法可以让它工作吗?我希望每次调整窗口大小时都调整列的大小以适应 QTableView 的边缘。
采纳答案by Davy Jones
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:
但是,这不会按比例调整其他列的大小。如果你也想这样做,你可以在你父母的 resizeEvent 中处理它:
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 的子类。
回答by frogatto
This code equally stretchs each columns so that they fit the table's width.
此代码平均拉伸每一列,使它们适合表格的宽度。
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
Docs:
文档:
- QHeaderView::setSectionResizeMode
- See resize modes here.
- QHeaderView::setSectionResizeMode
- 请参阅此处的调整大小模式。
回答by iamantony
Widgets QTableView, QTreeViewand 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 Jonesmentioned.Example:
QTableView *table = new QTableView(); table->horizontalHeader()->setStretchLastSection(true);
void QHeaderView::setResizeMode( ResizeMode mode )
As modeyou 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);
void QHeaderView::stretchLastSection( bool stretch )
正如戴维·琼斯所提到的。例子:
QTableView *table = new QTableView(); table->horizontalHeader()->setStretchLastSection(true);
void QHeaderView::setResizeMode( ResizeMode mode )
作为模式,您可以设置 QHeaderView::Stretch 或 QHeaderView::ResizeToContents。
不幸的是,这种方法有一个缺点——应用后,您将无法手动(在 GUI 中)或以编程方式更改列(或行)的大小。例子:
QTableView *table = new QTableView(); table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);