0

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.

Niklas
  • 23,674
  • 33
  • 131
  • 170

1 Answers1

2

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.

Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • How performant is `QPainter` since I may need to set a different text every 20-50ms? – Niklas Oct 07 '14 at 20:55
  • @Niklas it is fast, it is problem. I can't said it accurate now, but check my answer on next question(I added CPU load example), so you can approximate time(up to 7% CPU load when you move mouse very fast) http://stackoverflow.com/questions/25404709/how-to-make-a-qlineedit-follow-the-cursor-to-show-me-its-coordinates/25410887#25410887 – Jablonski Oct 07 '14 at 21:02
  • And QLabel supports HTML, but I don't know is it possible to to do it with html – Jablonski Oct 07 '14 at 21:05
  • So far `QPainter` seems to be fine for my case. Do you know whether I can draw the text aligned to the center or do I have to calculate that by myself? – Niklas Oct 07 '14 at 21:18
  • Okay there is a `paint.drawText(rect, Qt::AlignCenter, text);` function. Thats pretty neat. – Niklas Oct 07 '14 at 21:26
  • @Niklas The `QPainter` question doesn't make much sense since `QLabel` uses `QPainter` to paint itself :) As for what's possible with `QPainter`, you really need to read its documentation. The entire thing. – Kuba hasn't forgotten Monica Oct 08 '14 at 21:00
  • @KubaOber okay I did not know that. Your answer [here](http://stackoverflow.com/a/24831796/1979703) gave me pretty much everything I needed :) – Niklas Oct 08 '14 at 21:05