“text-overflow” for a QLabel’s text rendering in QT

前端 未结 3 1228
一个人的身影
一个人的身影 2020-12-14 17:20

I have got a QLabel element in a widget which can be resized. The text can overflow boundaries, so I need, for the application to look more elegant, some way to make the tex

3条回答
  •  既然无缘
    2020-12-14 17:54

    I've modified solution described above and created a function:

    static void SetTextToLabel(QLabel *label, QString text)
    {
        QFontMetrics metrix(label->font());
        int width = label->width() - 2;
        QString clippedText = metrix.elidedText(text, Qt::ElideRight, width);
        label->setText(clippedText);
    }
    

    Hope it will be useful.

提交回复
热议问题