[SOLVED]How to set text on a custom painted QLabel
-
I've created a custom painted QLabel widget and now want to set text on it. Calling it's setText() function doesnt work, the text does not get displayed. I've tried QPainter::drawText() but i need the label to automatically change it's size depending upon the length of the text in the label. What should i do?
-
You are asking two different things:
How to make QLabel render text when you reimplemented the paint method, and
how to make QLabel resize itself when the text set in it changes
I don't know why you reimplemented paintEvent, but if you want QLabel to display any text set, you should either render the set text yourself (indeed, using drawText inside the paintEvent method) or allow the QLabel base class to do it for you by calling QLabel::paintEvent(pe) inside your own implementation.
On your second question. This is not so easy to make work in all circumstances. The layout of a form is determined by more than just your label. However, you can manipulate the sizeHint and/or the minimumSize of your label.
-
I was trying to make a speech bubble like this
!http://3.bp.blogspot.com/-OWv-yQxf0gs/UM3lQ5zfJRI/AAAAAAAAANs/aewZXGcUQ-I/s1600/Screenshot_messages.png(bubble)!Calling QLabel::paintEvent() along with setMargin() did the trick, thanks