Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    4
    3352
    Loading More Posts
    • 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
      QPerson last edited by

      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 Reply Quote 0
      • A
        andre last edited by

        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 Reply Quote 0
        • musimbate
          musimbate last edited by

          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 Reply Quote 0
          • Q
            QPerson last edited by

            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 Reply Quote 0
            • First post
              Last post