C++ Qt QTableWidget 列调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15686501/
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
Qt QTableWidget Column resizing
提问by Normal People Scare Me
I have a MainWindowwith a QToolbar, QWidgetand a QTabWidget. The layout is "Grid". However, my window is resizeable and since I have a layout it works well. But there is one problem, in my QTabWidgetI have a QTableWidgetwith two columns (layout is also "Grid"). If I resize my whole window the QTableWidgetresizes but not the columns.
我有一个主窗口与在QToolBar,QWidget的和QTabWidget。布局是“网格”。但是,我的窗口可以调整大小,而且由于我有一个布局,所以效果很好。但是有一个问题,在我的QTabWidget 中,我有一个带有两列的QTableWidget(布局也是“网格”)。如果我调整整个窗口的大小,QTableWidget 会调整大小,但不会调整列的大小。
For example Whenever I resize my window, my QTabWidgetresizes and the QTableWidgetin it too. Only the columns in my QTableWidgetwon't.
例如,每当我调整窗口大小时,我的QTabWidget和其中的QTableWidget也会调整大小。只有我的QTableWidget 中的列不会。
So... how can I resize them if my QTableWidgetresizes?
那么...如果我的QTableWidget调整大小,我该如何调整它们的大小?
回答by PrisonMonkeys
- Change the
ResizeMode
of theQHeaderView
. For example, use:
- 更改
ResizeMode
的QHeaderView
。例如,使用:
horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
to make the first column resize so the QTableWidget
is always full.
使第一列调整大小,以便QTableWidget
始终充满。
- Override the
resizeEvent
and set the widths of each column yourself when theQTableWidget
has been resized.
- 调整大小后,
resizeEvent
自己覆盖并设置每列的宽度QTableWidget
。
回答by fat
To stretch last column:
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
To stretch column #n:
ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
拉伸最后一列:
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
拉伸列#n:
ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
回答by user4708486
ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
回答by Jhon Escobar
The best solution for this, in Qt5 you have to use setSectionResizeMode
instead of setResizeMode
对此的最佳解决方案,在 Qt5 中,您必须使用setSectionResizeMode
而不是setResizeMode
tabv = QTableView()
tabv.horizontalHeader().setSectionResizeMode(QHeaderView::Stretch)
Also you can specify the Stretch
mode when resizing
您也可以Stretch
在调整大小时指定模式
tabv.horizontalHeader().resizeSections(QHeaderView::Stretch)
回答by Romha Korev
You can change the "resize mode" of your columns or rows with the QHeaderView and the method QHeaderView::setResizeMode().
您可以使用 QHeaderView 和方法 QHeaderView::setResizeMode() 更改列或行的“调整大小模式”。
http://qt-project.org/doc/qt-4.8/qheaderview.html#setResizeMode
http://qt-project.org/doc/qt-4.8/qheaderview.html#setResizeMode
http://qt-project.org/doc/qt-4.8/qtableview.html#verticalHeader
http://qt-project.org/doc/qt-4.8/qtableview.html#verticalHeader
http://qt-project.org/doc/qt-4.8/qtableview.html#horizontalHeader
http://qt-project.org/doc/qt-4.8/qtableview.html#horizontalHeader
回答by GENiEBEN
If you want to resize just the last column:
如果您只想调整最后一列的大小:
ui->tableWidget->horizontalHeader()->setStretchLastSection(1);
回答by Niklas
In Qt5 you have to use setSectionResizeMode
instead of setResizeMode
在 Qt5 中,您必须使用setSectionResizeMode
而不是setResizeMode
QTableWidget* myTable = new QTableWidet;
QHeaderView* header = myTable->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);