Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a QTableView to fill 100% of the width?

Tags:

qt

qtableview

If you are using Qt 5, QHeaderView::setResizeMode() is no longer available. Instead, you can use QHeaderView::setSectionResizeMode():

ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

Or just call it for every column:

for (int c = 0; c < ui->tableView->horizontalHeader()->count(); ++c)
{
    ui->tableView->horizontalHeader()->setSectionResizeMode(
        c, QHeaderView::Stretch);
}

Use view->horizontalHeader()->setStretchLastSection(true) to make the last column expand to free space.

Additionally, use view->horizontalHeader()->setResizeMode(QHeaderView::Stretch) to give columns the same width.


Here works using only with:

ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

I'm using Qt 5.2!