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

5

43

Here's a print screen of my software:

As you can see, the first QTableVIew headers do not take 100% of the width. In fact, there is a small vertical white space on the right of the field size.

How can I get the headers to take 100% of the width of the QTableView?

Inapprehensive answered 8/7, 2013 at 20:37 Comment(1)
possible duplicate of QStandardItemModel inside QtableviewOrsino
B
56

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);
}
Bathelda answered 8/7, 2013 at 21:45 Comment(4)
Same problem, still doesn't work. Last section is stretched but not the first one.Inapprehensive
That works for me with Qt 5.1. Have you set the model to the tableview before setting the section resize modes with this code? If you haven't, you don't have any columns yet and this code does nothing.Bathelda
Don't call QHeaderView::setSectionResizeMode() for every column. To automatically apply the passed stretch to all columns, just call that method once without iteratively passing an explicit column index: e.g., ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);. The above for loop thus reduces to a simplistic one-liner. See also this relevant answer.Supernumerary
You cant adjust the widths after using Stretch for setSectionResizeMode(), can you?Coffer
S
26

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.

Sapsucker answered 8/7, 2013 at 20:43 Comment(2)
@Inapprehensive If you're using QtCreator/Designer you can find this under the properties sectionAlfaro
The first command works, but the second doesn't. That means now the last columns is stretched but the two columns are not the same size.Inapprehensive
M
16

Here works using only with:

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

I'm using Qt 5.2!

Mullens answered 26/3, 2014 at 20:11 Comment(0)
S
0

I had a hard time distributing column widths among all cells of a table. In my case, in headerData function of the model, I did the following (requires calling resizeColumnsToContents() somewhere):

QVariant headerData(int section, Qt::Orientation orientation, int role) const override {
  if (orientation == Qt::Vertical) {
    return QVariant();
  }
  if (role == Qt::SizeHintRole) {
    auto* p = qobject_cast<QTableView*>(QObject::parent());
    if (p == nullptr) return QVariant();
    // Parent total width.
    const int w = p->viewport()->size().width() -
        p->verticalScrollBar()->sizeHint().width();
    QSize qs;
    // Default height.
    qs.setHeight(p->verticalHeader()->defaultSectionSize());
    // Width per column.
    switch (section) {
      case 0:
        qs.setWidth(w * 0.45);
        return QVariant(qs);
      case 1:
        qs.setWidth(w * 0.45);
        return QVariant(qs);
      // ... others
      default: ;
    }
    return QVariant();
  }
  if (role == Qt::DisplayRole) {
    // header titles.
  }
}
Sayyid answered 10/9, 2017 at 16:1 Comment(0)
B
0

If anyone here is new to PyQt like me and really struggled to understand how to do this in Python using a QTableWidget. Here is an example with pyqt5.

table = QTableWidget()
header_view = QHeaderView(QtCore.Qt.Orientation.Horizontal, table)
header_view.setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
table.setHorizontalHeader(header_view)
Beguile answered 11/6 at 14:49 Comment(1)
Note that the question and the other answers are in C++ while this answer is for Python.Viehmann

© 2022 - 2024 — McMap. All rights reserved.