Important: Please read the Qt Code of Conduct -

Adjusting column width and height in QTableWidget

  • I have a QTableWidget with 2 columns. The first column is filled with a file name and the other column with some text through a QTextEdit.

    void StatisticDialog::addFilename(const QString &filename)
        int pos = ui->twStatistic->rowCount();
        ui->twStatistic->setRowCount( pos + 1 );
        ui->twStatistic->setItem( pos, 0, new QTableWidgetItem ( filename ) );
        ui->twStatistic->setColumnWidth( 0, 150 );
    void StatisticDialog::addResult(const QString &filename, const QString &res)
        for ( int pos = 0; pos < ui->twStatistic->rowCount(); ++pos )
            if ( ui->twStatistic->item( pos, 0 )->text() == filename )
                QTextEdit *textEdit = new QTextEdit;
                textEdit->setText( res );
                textEdit->setReadOnly ( true );
                ui->twStatistic->setCellWidget( pos, 1, textEdit );
                ui->twStatistic->resizeColumnToContents( 1 );
                ui->twStatistic->resizeRowToContents( pos );

    The second column width and row height remains in some sort of a standard sizes. I would like to the second column fitting the available space, but it doesn't. I guess the reason is the size of QTextEdit used.
    I had used already the QTextSize directly, but the results were even worse. This size seems even to vary with each QTextEdit being created. The result was that with every QTextEdit added, the column did shrink.

    Any suggestions?

  • Lifetime Qt Champion


    If the second column is also the last you could try with QHeaderView's stretchLastSection property

    Hope it helps

  • Yes, it does. Obviously looked in the wrong part.

    Thanks for help.

  • Lifetime Qt Champion

    That one is a bit hidden for sure

    You're welcome !