Results 1 to 8 of 8

Thread: Subclassing QPushButton to support word wrapping

  1. #1
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Subclassing QPushButton to support word wrapping

    I would like to subclass QPushButton so that it supports word wrapping similar to how QLabel word wraps it also still needs to support StyleSheets.

    I know I need to override the paintevent but I don't have any experience with low level painting and dealing with styles.

    This is the current QPushbutton paintevent

    Qt Code:
    1. void QPushButton::paintEvent(QPaintEvent *)
    2. {
    3. QStylePainter p(this);
    4. initStyleOption(&option);
    5. p.drawControl(QStyle::CE_PushButton, option);
    6. }
    To copy to clipboard, switch view to plain text mode 

    looking at this I think what I think I need to do is something like this

    Qt Code:
    1. void WrappingPushButton::paintEvent(QPaintEvent *)
    2. {
    3. QStylePainter p(this);
    4. initStyleOption(&option);
    5.  
    6. //How do I set the text style (font size etc) in the document to that defined on
    7. //the push button via a stylesheet?
    8. doc.setText(text());
    9. doc.setTextWidth(width());
    10.  
    11. //Is it safe to change the height of a widget within it's own paintevent?
    12. setHeight(doc.height());
    13.  
    14. //In this function how to prevent it from drawing the bad one lined text?
    15. p.drawControl(QStyle::CE_PushButton, option);
    16.  
    17. //How to get the innerRectangle of the QPushButton so that I don't draw over
    18. //any padding defined in the style.
    19. QRect innerRectangle;
    20.  
    21. QPainter painter(this);
    22. doc.drawContents( painter, innerRectangle );
    23. }
    To copy to clipboard, switch view to plain text mode 

    Guidance is welcome and if this approach is completely wrong please let me know that too

  2. #2
    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: Subclassing QPushButton to support word wrapping

    Close enough. You need to query QStyle API for things like margins and you'll also want to remove the text to draw from the "option" object. You also need to calculate the proper size of the button to hold all the text and return it from within 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.


  3. The following user says thank you to wysota for this useful post:

    Berryblue031 (9th June 2011)

  4. #3
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Subclassing QPushButton to support word wrapping

    Thank you Wysota

    I am having a small problem though setting the font on the QTextDocument.
    From QStyleOption I can get the QFontMetrics, but I can't figure out how to apply them to the QTextDocument which only has setFont(QFont).

    I dont want to use QPushButton::font() because I don't believe that takes stylesheet fonts into consideration.

  5. #4
    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: Subclassing QPushButton to support word wrapping

    QTextDocument usually has its own font that can be set independently from the widget. If you just want word wrapping then using QPainter::drawText() with the wrapping option enabled might be an easier solution. I don't think you'll be able to retain the stylesheet font anyway so I wouldn't bother trying.
    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.


  6. The following user says thank you to wysota for this useful post:

    Berryblue031 (9th June 2011)

  7. #5
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Subclassing QPushButton to support word wrapping

    Hmmm but without the QTextDocument how can I determine the necessary height of the widget?

  8. #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: Subclassing QPushButton to support word wrapping

    QFontMetrics::boundingRect() has the needed capabilities.
    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.


  9. The following user says thank you to wysota for this useful post:

    Berryblue031 (9th June 2011)

  10. #7
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Subclassing QPushButton to support word wrapping

    Ok I am soooo close, the only thing I am missing is how to find the actual padding on the QPushButton (i.e. the space between the buttons drawn border and the area for the text)

    Qt Code:
    1. QSize WrappingPushButton::sizeHint() const
    2. {
    3. QSize size = QPushButton::sizeHint();
    4. initStyleOption(&option);
    5.  
    6. QMargins m = getPaddingToLabel();
    7. QRect r = option.fontMetrics.boundingRect(0, 0, width() - m.right() - m.left(), 2147483647, mTextFlags, text());
    8.  
    9. size.setHeight(r.height() + m.top() + m.bottom());
    10.  
    11. return size;
    12. }
    13.  
    14. void WrappingPushButton::paintEvent(QPaintEvent* e)
    15. {
    16. QStylePainter p(this);
    17. initStyleOption(&option);
    18.  
    19. //This will prevent the style from drawing the text
    20. option.text = "";
    21.  
    22. //Draw button
    23. p.drawControl(QStyle::CE_PushButton, option);
    24.  
    25. //Draw Text
    26. QMargins m = getPaddingToLabel();
    27. QRect innerRectangle = QRect(m.right(), m.top(), width() - m.right() - m.left(), height() - m.top() - m.bottom());
    28. p.drawText(innerRectangle, mTextFlags, text());
    29. }
    30.  
    31. QMargins WrappingPushButton::getPaddingToLabel() const
    32. {
    33. //TODO: How to get the real margins!
    34. return QMargins(10,10,10,10);
    35. }
    To copy to clipboard, switch view to plain text mode 

  11. #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: Subclassing QPushButton to support word wrapping

    Last edited by wysota; 9th June 2011 at 15:26.
    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.


  12. The following user says thank you to wysota for this useful post:

    Berryblue031 (10th June 2011)

Similar Threads

  1. Subclassing QWidget and QPushButton
    By ber0y in forum Newbie
    Replies: 7
    Last Post: 24th July 2009, 10:25
  2. Does QT support Word automation?
    By diverger in forum General Programming
    Replies: 3
    Last Post: 12th September 2007, 08:20
  3. Word wrapping in a QTableWidget cell
    By jcooperddtd in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2007, 03:57
  4. Word wrapping
    By bruccutler in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2007, 16:33
  5. word wrapping in QTable Cell
    By joseph in forum General Discussion
    Replies: 0
    Last Post: 27th November 2006, 09:30

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.