我正在为应用程序创建自定义消息框。我的对象派生自 QMessageBox,但我重写了 paintEvent() 方法以更改其外观。奇怪的是,虽然我没有在派生方法中调用基本的 paintEvent 方法,但默认情况下我的自定义消息框仍然使用 OK 按钮绘制。这是我的代码:
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");
}
当我没有调用基本的 paintEvent 方法时,其他东西是如何被绘制的?
最佳答案
OK 按钮没有被绘制。它是添加到消息框的子 QWidget。子部件绘制不受父部件的 paintEvent 控制。
关于c++ - 覆盖 QMessageBox::paintEvent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5096077/