Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]How to set text on a custom painted QLabel

[SOLVED]How to set text on a custom painted QLabel

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QPerson
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • musimbateM Offline
        musimbateM Offline
        musimbate
        wrote on last edited by
        #3

        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.

        Why join the navy if you can be a pirate?-Steve Jobs

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          QPerson
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved