Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make QLabel width independent of text

I need a QLabel whose width should not adapt to the contained text but which is resizeable by the user (or the layout to be exact). If the text is too long for the width of the QLabel it should simply be clipped.

This question is somehow the reverse of How to make QLabel expand width geometry to accommodate text. However, the content of that question didn't help me. Neither did Setting text on a QLabel in a layout, doesn't resize.

Background

The QLabel will display identifiers (single words) coming from another system. Sometimes those identifiers change many times a second, which makes the whole layout flickering. The QLabel is part of a vertical dock so the width of the dock flickers.

On the other hand, it should be up to the user to decide how much of the identifiers s/he could see. So I want to allow the user to change the width of the dock such that the width of the QLabel adapts to that.

Solution attempts

To achieve this I set the horizontal size policy to QSizePolicy::Preferred and derived my own label class from QLabel in which I've overridden sizeHint() to return a fixed size. But that didn't change the behavior.

I know I could apply QFontMetrics to compute the width of the text and then cut it off to fit into the QLabel width. But that seems not to be the right solution, particularly as I would like to have the last letter itself clipped if it does not fit in entirely to give the user the clue that the identifier is too long to be displayed.

Versions

  • Qt 5.5.1
  • GCC 5.4.0
  • All from current Ubuntu 16.04 repository
like image 203
bjhend Avatar asked Mar 06 '23 23:03

bjhend


2 Answers

Scaling the text is not a good idea, as scaled text will be hardly visible in case of long strings and small labels. As an alternative I would put my label in a scroll area so it can hold label of any size without resizing itself (and prevent my GUI from flickering). Here is a simple example how to to it:

QLabel *label = new QLabel;
label->setAlignment(Qt::AlignTop);

QScrollArea *scrollArea = new QScrollArea;
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(label);

label->setText("ThisIsVeryLargeStringThatIWantToPutIntoALabel");
scrollArea->show();

This scroll area can lay into a dockable window.

like image 117
vahancho Avatar answered Mar 16 '23 14:03

vahancho


I think i found a rather dirty solution to your problem which may cause more issues, but you could try it. It simply prevents setText from resizing the label, while it still allows the user and layout to resize.

void CustomLabel::setText(const QString text)
{
    max = maximumSize();
    min = minimumSize();
    setMinimumSize(size());
    setMaximumSize(size());
    settingText = true;

    QLabel::setText(text);
}


void CustomLabel::resizeEvent(QResizeEvent *event)
{
    QLabel::resizeEvent(event);
    if(settingText){
        setMinimumSize(min);
        setMaximumSize(max);
        settingText = false;
    }
}
like image 20
Ole Avatar answered Mar 16 '23 14:03

Ole