Important: Please read the Qt Code of Conduct -

[SOLVED] Scaling QLabel to accommodate the contained text and nothing more

  • I'm subclassing QLabel and inserting it into a QVBoxLayout and I'm having a problem with the size. The following screenshot is how my application currently looks:


    The yellow widget on the top is my QLabel, and the layout is the default one. I'd like my label to scale down to fit the text without manually setting it with setMinimumHeight(), however that wouldn't allow me to use strings longer than the width of the label. I've checked the documentation for QString (which is what I am passing to the label's constructor) and I can't find a way to extract the size of the text. Is there any way I can dynamically change the size of my label to fit the text, with no excess space?

  • see following method's documentation

    QRect QFontMetrics::boundingRect ( const QRect & rect, int flags, const QString & text, int tabStops = 0, int * tabArray = 0 ) const

    especially be noted the flags being used for examples

    Qt::AlignHCenter and Qt::AlignVCenter(which is Qt::AlignCenter)


    BTW, it looks like kanban board :)

  • Thanks a lot for your help. That's exactly what I've been looking for.

    bq. BTW, it looks like kanban board :)

    Well, as it so happens... ;)

Log in to reply