Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt QTableWidget Column resizing

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?

like image 304
Normal People Scare Me Avatar asked Mar 28 '13 15:03

Normal People Scare Me


4 Answers

  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.
like image 95
PrisonMonkeys Avatar answered Nov 17 '22 22:11

PrisonMonkeys


  1. To stretch last column:

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

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch);
    
like image 27
fat Avatar answered Nov 17 '22 23:11

fat


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)
like image 6
Jhon Escobar Avatar answered Nov 17 '22 23:11

Jhon Escobar


ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
like image 4
user4708486 Avatar answered Nov 17 '22 22:11

user4708486