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
{
initStyleOption(&option);
p.
drawControl(QStyle::CE_PushButton, option
);
}
void QPushButton::paintEvent(QPaintEvent *)
{
QStylePainter p(this);
QStyleOptionButton option;
initStyleOption(&option);
p.drawControl(QStyle::CE_PushButton, option);
}
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
{
initStyleOption(&option);
//How do I set the text style (font size etc) in the document to that defined on
//the push button via a stylesheet?
doc.setText(text());
doc.setTextWidth(width());
//Is it safe to change the height of a widget within it's own paintevent?
setHeight(doc.height());
//In this function how to prevent it from drawing the bad one lined text?
p.
drawControl(QStyle::CE_PushButton, option
);
//How to get the innerRectangle of the QPushButton so that I don't draw over
//any padding defined in the style.
doc.drawContents( painter, innerRectangle );
}
void WrappingPushButton::paintEvent(QPaintEvent *)
{
QStylePainter p(this);
QStyleOptionButton option;
initStyleOption(&option);
QTextDocument doc;
//How do I set the text style (font size etc) in the document to that defined on
//the push button via a stylesheet?
doc.setText(text());
doc.setTextWidth(width());
//Is it safe to change the height of a widget within it's own paintevent?
setHeight(doc.height());
//In this function how to prevent it from drawing the bad one lined text?
p.drawControl(QStyle::CE_PushButton, option);
//How to get the innerRectangle of the QPushButton so that I don't draw over
//any padding defined in the style.
QRect innerRectangle;
QPainter painter(this);
doc.drawContents( painter, innerRectangle );
}
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
Bookmarks