I am creating a custom message box for an application. My object is derived from QMessageBox, but I am overriding the paintEvent() method in order to change its appearance. Curiously, although I do not call the base paintEvent method in my derived method, my custom message box is still being painted with an OK button by default. Here is my code:
class MessageWidget : public QMessageBox
{
Q_OBJECT
public:
MessageWidget(QWidget* parent = 0);
~MessageWidget();
void setTitle(const QString& title);
const QString& title() const;
protected:
void paintEvent(QPaintEvent* event);
}
MessageWidget::MessageWidget(QWidget* parent) :
QMessageBox(parent)
{
setWindowFlags(Qt::FramelessWindowHint);
setAutoFillBackground(true);
}
void MessageWidget::paintEvent(QPaintEvent* /*event*/)
{
QPainter painter(this);
QRect boxRect = rect();
QPainterPath path;
path.addRoundedRect(boxRect, 15, 15);
painter.开发者_运维技巧fillPath(path, palette().window());
painter.drawPath(path);
QRect titleRect = boxRect;
int titleHeight = fontMetrics().height();
titleRect.moveBottom(titleHeight);
boxRect.moveTop(titleRect.bottom());
painter.drawLine(titleRect.bottomLeft(), titleRect.bottomRight());
painter.drawText(titleRect, Qt::AlignLeft, "Some Text");
}
How is it that other things are being painted when I'm not calling the base paintEvent method?
The OK button is not being painted. It's a child QWidget added to the message box. Child widget painting is not controlled in the parent's paintEvent.
精彩评论