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