Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Adjust QListWidget size to content



  • Hello,
    I've been trying to adjust the size of QListWidget to its contents (QListWidgetItems with text); solutions found in google either complicated (in my opinion) for such a trivial task or given little to no explanation or code snippets. I guess there's an easy and elegant solution but still have no clue.
    Thanks in advance.


  • Lifetime Qt Champion

    Hi,

    One possible source of information is QListWidget::sizeHintForColumn. You can use that to resize your widget.



  • @SGaist,
    Got it.
    Here's the code snippet in case anyone's looking for an answer:

    ui->listWidget->setFixedSize(ui->listWidget->sizeHintForColumn(0) + ui->listWidget->frameWidth() * 2, ui->listWidget->sizeHintForRow(0) * ui->listWidget->count() + 2 * ui->listWidget->frameWidth());
    

Log in to reply