Results 1 to 16 of 16

Thread: QPushbotton - WordWrap

  1. #1
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question QPushbotton - WordWrap

    I need the text in the QPushbutton to wrap automatically when it is more than the button size. I cannot increase the width of the button as there is no space for it. kindly suggest a way.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushbotton - WordWrap

    You will have to take the button text, the button width and the button font metrics and insert manually "\n"-s in the string.

    Or you can use an image for the button and leave the text for the tooltip.

    Regards

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushbotton - WordWrap

    Or you can subclass the button and draw it yourself using proper style primitives.

  4. #4
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPushbotton - WordWrap

    thanks for the replies..
    Actually I am working on the internatiolisation project and the pushbutton text may vary according to the language use.
    The option of manually inserting the '\n' by calculating the width of the button and the text size looked to be a complicated work, So i thought may be there is something like QLable wordwrap feature that we could use for QPushbutton as well.
    But the more I surfed the net I am getting a feeling that the only option is by manual insertion of new line character.
    Please advise

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushbotton - WordWrap

    But it is not that hard.
    Assuming that the character size in pixels for the current button font is x pixels, then the maximum size of the text is textLength*x=T.

    Given the width of the button, lets say W:
    float Y = T/W.

    If Y <= 1.0 then the text completely fits.
    If Y > 1.0 then you must insert ~Y "\n"s every ~W/x characters.

    T, Y and W must be real numbers. When you need to compute the "\n" indexes just approximate through an integer.

    Regadrs

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushbotton - WordWrap

    I wouldn't do any manual inserts. If the size of the container changes, you'd have to re-layout those \n characters again. It's much simpler to subclass and ask QPainter::drawText to wrap the text. But maybe there are better ways. If not, I suggest subclassing instead of manual changes of the text.

  7. #7
    Join Date
    Jun 2007
    Posts
    28
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPushbotton - WordWrap

    Thanks Marcel,
    I had done a lot of calculation to get the desired result, but I will try your solution today.
    Wysota, regarding the relayout problem , the problem started because the size of the container could not be changed as it is already occupying maximum space. Regarding your solution of subclassing and using drawtext function, I am still not clear as how that would lead to wordwrap of the text on the button. Can you please elaborate more.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushbotton - WordWrap

    Quote Originally Posted by anju123 View Post
    Wysota, regarding the relayout problem , the problem started because the size of the container could not be changed as it is already occupying maximum space.
    What if you want to decrease the size, the font size or application style changes?

    Regarding your solution of subclassing and using drawtext function, I am still not clear as how that would lead to wordwrap of the text on the button. Can you please elaborate more.
    There are drawText variants that do word wrapping, so essentially it's just a matter of asking the style to draw the button itself without the text and then draw the text using drawText, passing the rect() of the button (maybe adjusted a little to have a margin) as the rectangle the text should occupy.

    Example result attached.
    Attached Images Attached Images
    Last edited by wysota; 14th June 2007 at 09:16.

  9. #9
    Join Date
    Jul 2008
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPushbotton - WordWrap

    Hello,

    As I am still looking for a clean method to wrap the text in a QPushButton can the Guru or someone else, that have managed to deal with the problem, post the exact code to wrap the text in one QPushButton.

    I am really waiting for your response as I don't have any clear ideas of what to do because of the numerous unclear answers given in all the forums about qt.

    Thanks a lot by advance,
    Hub++

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushbotton - WordWrap

    It depends what you want to do. Adding newlines (\n) to your text should force wrapping it at that point. If you want the flow to adjust depending on the width of the button, you need to subclass QPushButton and reimplement the part responsible for drawing the text and calculating the size of the button. For drawing you need to use a QPainter::drawText() overload that takes flags as one of its parameters and pass the wrapping flag.

  11. #11
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushbotton - WordWrap

    Hello, sorry for bringing this up after so long, but I think this is exactly what I need and I want to make sure I am understanding this correctly.

    I am derivating from QPushButton, but want to wrap text, so ths is what I did:

    Qt Code:
    1. class NEWPushButton : public QPushButton
    2. {
    3. Q_OBJECT
    4. public:
    5. void drawText ( const QRect & rectangle, int flags, const QString & text, QRect * boundingRect = 0 )
    6. {
    7. QPainter::drawText ( rectanble, flags, text, boundingRect );
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 

    is this what you had in mind, Wysota? I am guessing the proper "boundingRect" would be the button's (which is what I want the text to wrap to, after all).

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushbotton - WordWrap

    Making that method public doesn't make much sense and the syntax is incorrect, however the approach itself seems ok.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushbotton - WordWrap

    You are totally right, I was too focused on the approach iteself and made silly mistakes.

    Here is the (working) code I came up with using your suggestion, for future reference and archiving:

    Qt Code:
    1. class NEWPushButton : public QPushButton
    2. {
    3. Q_OBJECT
    4. private:
    5. void drawText ( const QRect & rectangle, int flags, const QString & text, QRect * boundingRect = 0 )
    6. {
    7. QPainter painter;
    8. painter.drawText ( rectanble, flags, text, boundingRect );
    9. }
    10. };
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushbotton - WordWrap

    Should be:

    Qt Code:
    1. QPainter painter(this);
    To copy to clipboard, switch view to plain text mode 

    Also be sure to reimplement sizeHint().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushbotton - WordWrap

    Quote Originally Posted by wysota View Post
    Also be sure to reimplement sizeHint().
    Why? I haven't compiled it yet on the real application (a big solution).

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPushbotton - WordWrap

    Because size of the text with wordwrapping is different than the one without word wrapping.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. wordwrap question -2
    By igor_x in forum Newbie
    Replies: 0
    Last Post: 3rd November 2006, 06:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.