Pergunta

I have the following structure:

QScrollArea
  - QWidget (with QHBoxLayout)
    - QLabel
    - QLabel
    - QLabel
    ...

QLabels are generated automatically with random text. How to change the size of the widget automatically to fit labels? At the moment I can see the widget only by setting a fixed size.

Solution:

After creating QLabel I store its width:

int widthSum = 0;
for ( ... ){
    ...
    widthSum += label->sizeHint().width();
}

And after the cycle I set fixed width to widget:

widget->setFixedWidth(widthSum);

Thanks @shobi for a hint!

Foi útil?

Solução

use reSize() method on widget .

resize(sizeHint()) 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top