7

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.

How can I resize them if my QTableWidget resizes?

7 Answers 7

18
  1. Change the ResizeMode of the QHeaderView. For example, use:

horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );

to make the first column resize so the QTableWidget is always full.


  1. Override the resizeEvent and set the widths of each column yourself when the QTableWidget has been resized.
4
  • In which function do I set the horizontalheader? Commented Mar 28, 2013 at 16:20
  • 1
    "Tables can be given both horizontal and vertical headers. The simplest way to create the headers is to supply a list of strings to the setHorizontalHeaderLabels() and setVerticalHeaderLabels() functions." See QTableWidget details for this Commented Mar 28, 2013 at 16:30
  • in QT5: horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch );
    – Fantastory
    Commented Jun 3, 2014 at 9:01
  • 3
    No in Qt5, it's horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); The setResizeMode() method does not exist. Commented Jan 29, 2016 at 9:50
11
  1. To stretch last column:

    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    
  2. To stretch column #n:

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
    
0
6

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)
4
ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
0
2

If you want to resize just the last column:

ui->tableWidget->horizontalHeader()->setStretchLastSection(1);
1

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

0
1

In Qt5 you have to use setSectionResizeMode instead of setResizeMode

QTableWidget* myTable = new QTableWidet;
QHeaderView* header = myTable->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.