Qt World Summit: Submit your Presentation

QTableWidget::resizeRowsToContents() doesn't work until user's manual action

  • I populate my QTableWidget with ComboBoxes:

    w = new QWidget;
    ui->tableWidget->setCellWidget(row, col, w);
    lay = new QHBoxLayout(w);
    box->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    lay->addWidget(box, 0, Qt::AlignCenter);

    I call this function several times before I show QTableWidget to the user, and before that I do:


    QComboBoxes are visible in full and rows are a little wider than default (as to be expected). Now I call the functions again (with user looking) and the newly added row is stuck on default height (so it's too narrow for QComboBox to look good). After user clicks the vertical header, the row instantly snaps into the correct height fixing the visibility problem. What can I do to force QTableWidget to do this automatically?

  • @sm.sabat

    Can you try setResizeMode(QHeaderView::ResizeToContents)?

  • Lifetime Qt Champion

    Maybe this is related to https://bugreports.qt.io/browse/QTBUG-69563 ? But this only affects 5.11 and up.

Log in to reply