qtqlabelqstylesheet

QLabel add 'border' around text


Is it possible to add a slight 'border' atop and below the text displayed in a QLabel? The border should not be at the edge of the QLabel. Instead it should have a padding of something like 10px to the displayed text. The size of the border would be 75% of the QLabel width, starting at 12.5%. Also if the text changes, the 'border' should stay the same:

enter image description here

Furthermore it should still be possible to set the background color of the QLabel via a QStylesheet or some other API.


Solution

  • Too many specific things to do it easy. If you will not find better approach, use next:

    You can draw all your things on pixmap using QPainter and set this pixmap to label. For text: drawText, for line drawLine and use geometry of label to set needed size.

    About background:

    Still you should draw this things but before this use fill() method to fill pixmap with some color.