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.
}
}