Make QLabel Width Independent of Text

I need a QLabelwidth that should not adapt to the contained text, but can be changed by the user (or, more precisely, the layout). If the text is too long for the width QLabel, you should just crop it.

This question is somehow the opposite How to make QLabel the width of the width for placing text . However, the content of this question did not help me. Also, the installation of text in QLabel in the layout does not change.

Background

The QLabelidentifiers (single words) coming from another system will be displayed in. Sometimes these identifiers change many times per second, which makes the entire layout flicker. QLabelis part of the vertical dock, so the width of the dock flickers.

On the other hand, the user must decide how many identifiers he could see. Therefore, I want to allow the user to change the width of the dock so that the width QLabeladapts to this.

Resolving Solutions

To do this, I set the horizontal size policy to QSizePolicy::Preferredand got my own shortcut class from QLabel, in which I redefined sizeHint()to return a fixed size. But that did not change the behavior.

I know that I could apply QFontMetricsto calculate the width of the text, and then cut it off to fit the width QLabel. But this does not seem to be the right solution, especially since I would like the last letter itself to be truncated if it is completely inappropriate to let the user know that the identifier is too long to display.

  • Qt 5.5.1
  • GCC 5.4.0
  • Ubuntu 16.04
+4
3

- , . , ( ). :

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

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

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

.

+1

, , , . setText , - .

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;
    }
}
+3

, QWidget, :

#include <QWidget>

class DisplayWidget : public QWidget
{
    Q_OBJECT
    QString _text;
public:
    explicit DisplayWidget(QString text, QWidget *parent = nullptr);

    QString text() const;
    void setText(QString text);

protected:
    void paintEvent(QPaintEvent *event);
};

:

#include "displaywidget.h"

#include <QPainter>
#include <QFontMetrics>

bool DisplayWidget::ellipsis() const { return _ellipsis; }
void DisplayWidget::setEllipsis(bool ellipsis) { _ellipsis = ellipsis; }

DisplayWidget::DisplayWidget(QString text, QWidget *parent) : QWidget(parent), _text(text), _ellipsis(false) {}

QString DisplayWidget::text() const { return _text; }

void DisplayWidget::setText(QString text)
{
    _text = text;
    update();
}

void DisplayWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    QFontMetrics metrics(painter.font());

    int maxwidth = rect().width();
    QString text = _text;
    int length = _text.size();
    while(length > 0 && metrics.width(text, length) > maxwidth)
    {
        --length;
    }
    if(length < _text.size())
    {
        text = text.left(length);
        if(_ellipsis)
        {
            const QString ellipsis = " ...";
            maxwidth -= metrics.width(ellipsis);
            while(length > 0 && metrics.width(text, length) > maxwidth)
            {
                --length;
            }
            if(length > 0)
            {
                text = text.left(length);
            }
            else
            {
                text = "";
            }
            text.append(ellipsis);
        }
    }
    painter.drawText(rect(), Qt::AlignLeft, text);
}

, paintEvent. , , ( ) . -. , .

+2

Source: https://habr.com/ru/post/1693241/


All Articles