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?
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.To stretch last column:
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
To stretch column #n:
ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
The best solution for this, in Qt5 you have to use setSectionResizeMode
instead of setResizeMode
tabv = QTableView()
tabv.horizontalHeader().setSectionResizeMode(QHeaderView::Stretch)
Also you can specify the Stretch
mode when resizing
tabv.horizontalHeader().resizeSections(QHeaderView::Stretch)
ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With