Important: Please read the Qt Code of Conduct -

[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.

  • Would you care to post the code here so that we can have a glimple at what you are trying to achieve.I think it can make your question more clear.Remember to put your code between two @'s so it is readable.

  • I was trying to make a speech bubble like this

    Calling QLabel::paintEvent() along with setMargin() did the trick, thanks

Log in to reply