[Qt-interest] adapt the size of a QLabel inside a QVBoxLayout

Girish Ramakrishnan girish at forwardbias.in
Thu Feb 26 06:24:01 CET 2009


Markus Franke wrote:
> Hi Girish,
> 
> thanks for your reply. I understand what you mean. Below you see my
> sample code of an error dialog which is derived from SebaDialog which
> inherits QDialog. If the errortext in the pErrorText Label is too long
> the layout doesn't change it's size accordingly.
> 
> ---snip---
> ErrorMsgDialog::ErrorMsgDialog(QWidget* pParent, Error_Type type,
> QString text, bool cancelButton)
>     : SebaDialog(pParent, SebaDialog::SIZE_CUSTOM)
> {
>     QHBoxLayout* pMainLayout = new QHBoxLayout;
>     QVBoxLayout* pRightLayout = new QVBoxLayout;
>     QHBoxLayout* pLowerRightLayout = new QHBoxLayout;
> 
>     QString ImagePath;
>     switch(type)
>     {
>         case TYPE_FATAL:
>             ImagePath = getImagePath() + "Error_Fatal";
>             break;
>         case TYPE_ERROR:
>             ImagePath = getImagePath() + "Error_Normal";
>             break;
>         case TYPE_WARNING:
>             ImagePath = getImagePath() + "Error_Warning";
>             break;
>         case TYPE_INFO:
>             ImagePath = getImagePath() + "success";
>             break;
>     }
> 
>     QLabel* pIconLabel = new QLabel(this);
>     pIconLabel->setPixmap(QPixmap::fromImage(QIMAGE(ImagePath)));
> 
>     QLabel* pErrorText = new QLabel(text, this);
>     pErrorText->setWordWrap(true);
>     //pErrorText->adjustSize();
> 
>     SebaButton* pOkButton = new SebaButton(qApp->translate("Dialog",
> "Ok"), this);
>     QObject::connect(pOkButton, SIGNAL(clicked()), this, SLOT(accept()));
> 
>     pOkButton->setFocus();
> 
>     pMainLayout->addWidget(pIconLabel);
>     pRightLayout->addWidget(pErrorText);
> 
>     //pErrorText->setSizePolicy(QSizePolicy::Expanding,
> QSizePolicy::Expanding);
> 
>     if(cancelButton)
>     {
>         SebaButton* pCancelButton = new
> SebaButton(qApp->translate("Dialog", "Cancel"), this);
>         QObject::connect(pCancelButton, SIGNAL(clicked()), this,
> SLOT(reject()));
> 
>         pLowerRightLayout->addWidget(pOkButton);
>         pLowerRightLayout->addWidget(pCancelButton);
>     }
>     else
>     {
>         int spacing = 40;
>         pLowerRightLayout->addSpacing(spacing);
>         pLowerRightLayout->addWidget(pOkButton);
>         pLowerRightLayout->addSpacing(spacing);
>     }
> 
>     pRightLayout->addLayout(pLowerRightLayout);
>     pMainLayout->addLayout(pRightLayout);
> 
>     this->setLayout(pMainLayout);
> }

mm, you mean the window/dialog doesn't resize itself. In theory, it
should the window should expand but, as you say, it doesn't.

You can do the below:

1. set FixedSizeContraint of the main layout. This will make the dialog
fixed but will also make the layout increase/decrease the window size.

2. After you set the labels new text, resize the dialog yourself.
Something like:
        pMainLayout->activate();
        int height = pMainLayout->totalHeightForWidth(size().width());
// wrapping label has height for width
        resize(size().width(), height);

That said, you can try to use a grid layout instead. Step 2 will only
increase height which means at some point the dialog won't look
"beautiful". You will have to compute a nice 'width' yourself for that.
Take a look at qmessagebox.cpp for inspiration for beautiful sizing.

Girish



More information about the Qt-interest-old mailing list