I have a MainWindow with a QToolbar , QWidget and 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 QTabWidget I have a QTableWidget with two columns (layout is also "Grid"). If I resize my whole window the QTableWidget resizes but not the columns.
For example Whenever I resize my window, my QTabWidget resizes and the QTableWidget in it too. Only the columns in my QTableWidget won't.
So... how can I resize them if my QTableWidget resizes?
To stretch last column:
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
To stretch column #n :
ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
ResizeMode
of the QHeaderView
. For example, use: horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
to make the first column resize so the QTableWidget
is always full.
resizeEvent
and set the widths of each column yourself when the QTableWidget
has been resized. ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
You can change the "resize mode" of your columns or rows with the QHeaderView and the method QHeaderView::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#horizontalHeader
如果您只想调整最后一列:
ui->tableWidget->horizontalHeader()->setStretchLastSection(1);
The best solution for this, in Qt5 you have to use setSectionResizeMode
instead of setResizeMode
tabv = QTableView()
tabv.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
In Qt5 you have to use setSectionResizeMode
instead of setResizeMode
QTableWidget* myTable = new QTableWidet;
QHeaderView* header = myTable->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.