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.

So... how can I resize them if my QTableWidget resizes?

7 Answers7

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.
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
PrisonMonkeys
  • 1,199
  • 1
  • 10
  • 20
  • In which function do I set the horizontalheader? – Normal People Scare Me Mar 28 '13 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](http://qt-project.org/doc/qt-4.8/qtablewidget.html#details) for this – PrisonMonkeys Mar 28 '13 at 16:30
  • in QT5: horizontalHeader()->setResizeMode( 0, QHeaderView::Stretch ); – Fantastory Jun 03 '14 at 09:01
  • 3
    No in Qt5, it's horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); The setResizeMode() method does not exist. – Alexandre D. Jan 29 '16 at 09:50
11
  1. To stretch last column:

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

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
    
fat
  • 6,435
  • 5
  • 44
  • 70
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)
coterobarros
  • 941
  • 1
  • 16
  • 25
Jhon Escobar
  • 345
  • 3
  • 10
4
ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
  • 3
    Avoid code-only answers, they are usually flagged as low quality and removed. Try to put some effort into your answers and explain what the code does and how it would apply to the original question. – Tim Lewis Mar 24 '15 at 17:12
2

If you want to resize just the last column:

ui->tableWidget->horizontalHeader()->setStretchLastSection(1);
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
GENiEBEN
  • 186
  • 5
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

Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37
1

In Qt5 you have to use setSectionResizeMode instead of setResizeMode

QTableWidget* myTable = new QTableWidet;
QHeaderView* header = myTable->horizontalHeader();
header->setSectionResizeMode(QHeaderView::Stretch);
Niklas
  • 23,674
  • 33
  • 131
  • 170