QListWidget adjust size to content

前端 未结 6 917
情话喂你
情话喂你 2020-12-05 09:48

Is it possible to adjust QListWidget height and width to it\'s content?

sizeHint() always returns 256, 192 no matter what its content is.

6条回答
  •  温柔的废话
    2020-12-05 10:12

    First you should get your largest string in the list, that is easy to obtain.

    After you get that string, do the following:

    QFontMetrics * fm = new QFontMetrics(widget->font());
    QRect rect;
    rect = fm->boundingRect(string);
    

    rect.width() has the width in pixels of the largest string rect.height() has it's height.

    Set the QListWidget width to that rect's width (plus the margins) and it's height to that rect's height times the number of items

    I didn't test the code, but hope it puts you on the right track

提交回复
热议问题