Columns auto-resize to the size of QTableView
Asked Answered
A

4

57

I have just managed to make a QTableView work with my model. It has fixed 3 columns. When I open a window, it looks ok, but when I resize the window, the QTableView itself gets resized, but columns' width remains the same.

I want columns to resize to fit the edges of QTableView every time the window gets resized.

Is there any built-in way to make it work?

Appeal answered 17/8, 2013 at 21:13 Comment(0)
B
53

There is a header flag to ensure that the QTableView's last column fills up its parent if resized. You can set it like so:

table_view->horizontalHeader()->setStretchLastSection(true);

However, that does not resize the other columns proportionately. If you want to do that as well, you could handle it inside the resizeEvent of your parent thusly:

void QParent::resizeEvent(QResizeEvent *event) {
    table_view->setColumnWidth(0, this->width()/3);
    table_view->setColumnWidth(1, this->width()/3);
    table_view->setColumnWidth(2, this->width()/3);

    QMainWindow::resizeEvent(event);
}

QParent class is subclass of QMainWindow.

Bielefeld answered 18/8, 2013 at 5:47 Comment(0)
C
103

This code equally stretches each column so that they fit the table's width.

table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

Docs:

Cowley answered 9/12, 2015 at 22:19 Comment(0)
B
53

There is a header flag to ensure that the QTableView's last column fills up its parent if resized. You can set it like so:

table_view->horizontalHeader()->setStretchLastSection(true);

However, that does not resize the other columns proportionately. If you want to do that as well, you could handle it inside the resizeEvent of your parent thusly:

void QParent::resizeEvent(QResizeEvent *event) {
    table_view->setColumnWidth(0, this->width()/3);
    table_view->setColumnWidth(1, this->width()/3);
    table_view->setColumnWidth(2, this->width()/3);

    QMainWindow::resizeEvent(event);
}

QParent class is subclass of QMainWindow.

Bielefeld answered 18/8, 2013 at 5:47 Comment(0)
O
8

Widgets QTableView, QTreeView and their derived classes (such as QTableWidget) have this two usefull methods:

QHeaderView* horizontalHeader() const;
QHeaderView* verticalHeader() const;

If you open documentation for a class QHeaderView, you will find methods that set up appearance and behavior of row or column header for item views. You can resolve your problem by one of these methods:

  1. void QHeaderView::stretchLastSection( bool stretch )
    As Davy Jones mentioned.

    Example:

    QTableView *table = new QTableView();  
    table->horizontalHeader()->setStretchLastSection(true);
    
  2. void QHeaderView::setResizeMode( ResizeMode mode )
    As mode you can set QHeaderView::Stretch or QHeaderView::ResizeToContents.
    Unfortunately this method have a drawback - after it's apply you will not be able to change size of columns (or rows) manually (in GUI) or programmatically.

    Example:

    QTableView *table = new QTableView();  
    table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    
Obituary answered 26/12, 2014 at 14:13 Comment(0)
A
6

In PyQt5 you can achieve this in your table_widget by doing:

header = table_widget.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
Anastrophe answered 14/8, 2020 at 11:39 Comment(2)
If you are using PyQt6, it becomes: header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.ResizeToContents)Futility
This works very slow. It takes about 1 second to repaint 10000-20000 items.Denton

© 2022 - 2024 — McMap. All rights reserved.