简体   繁体   中英

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?

  1. To stretch last column:

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

     ui->tableWidget->horizontalHeader()->setSectionResizeMode(n, QHeaderView::Stretch); 
  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.
ui->mytable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

如果您只想调整最后一列:

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

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

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

In Qt5 you have to use setSectionResizeMode instead of setResizeMode

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM