Resizing a Qt QTableWidget Column

I have MainWindow with QToolbar, QWidget and QTabWidget. Layout - "Grid". However, my window is mutable, and since I have a layout, it works well. But there is one problem, in my QTabWidget I have a QTableWidget with two columns (the layout is also a "grid"). If I resize the whole window, QTableWidget resizes, but not the columns.

For example, whenever I resize my window, my QTabWidget changes and the QTableWidget in it too. Only columns in my QTableWidget will not.

So ... how to resize them if resizing a QTableWidget?

+6
source share
7 answers
  • Change the ResizeMode QHeaderView . For example, use:

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

to resize the first column so that the QTableWidget always full.


  1. Discard resizeEvent and set the width of each column yourself when the QTableWidget been resized.
+11
source
  • To stretch the last column:

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

     ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch); 
+11
source
 ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); 
+3
source

The best solution for this, in Qt5 you should use setSectionResizeMode instead of setResizeMode

 tabv = QTableView() tabv.horizontalHeader().setSectionResizeMode(QHeaderView::Stretch) 

You can also specify Stretch mode when resizing

 tabv.horizontalHeader().resizeSections(QHeaderView::Stretch) 
+2
source
+1
source

If you want to resize only the last column:

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

In Qt5, you should use setSectionResizeMode instead of setResizeMode

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

All Articles