40

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?

AAEM
  • 1,837
  • 2
  • 18
  • 26
  • 1
    possible duplicate of [QStandardItemModel inside Qtableview](http://stackoverflow.com/questions/16931569/qstandarditemmodel-inside-qtableview) – liuyi.luo Jul 09 '13 at 04:51

4 Answers4

54

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);
}
kimbaudi
  • 13,655
  • 9
  • 62
  • 74
  • 1
    Same problem, still doesn't work. Last section is stretched but not the first one. –  Jul 09 '13 at 09:31
  • 3
    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. –  Jul 09 '13 at 11:05
  • 11
    **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](https://stackoverflow.com/a/34190094/2809027). – Cecil Curry Feb 28 '18 at 09:12
  • You cant adjust the widths after using `Stretch` for `setSectionResizeMode()`, can you? – Jason Sep 13 '18 at 02:04
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.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • 3
    @user2429940 If you're using QtCreator/Designer you can find this under the properties section – Son-Huy Pham Jul 08 '13 at 20:50
  • 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. –  Jul 08 '13 at 21:11
14

Here works using only with:

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

I'm using Qt 5.2!

Dante
  • 742
  • 6
  • 10
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.
  }
}
Matferib
  • 318
  • 3
  • 10