Qt QPainter 绘制 QDialog 背景颜色和圆角边框
使用 QPainter 在 paintEvent 方法中绘制 QDialog 的背景颜色和边框圆角效果,可以按照以下步骤进行:
- 在 QDialog 类中重写 paintEvent 方法:
void CallReminderDialog::paintEvent(QPaintEvent *event)
{
QDialog::paintEvent(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 设置抗锯齿,使绘制更平滑
// 绘制背景颜色
QBrush brush(QColor(20, 25, 29));
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.drawRect(rect());
// 绘制边框圆角
QPainterPath path;
path.addRoundedRect(rect(), 20, 20);
painter.setClipPath(path);
painter.drawPath(path);
}
- 这段代码假设你已经在 QDialog 的子类中使用了 Q_OBJECT 宏,并且在头文件中声明了 paintEvent 方法:
class CallReminderDialog : public QDialog
{
Q_OBJECT
public:
explicit CallReminderDialog(QWidget *parent = nullptr);
protected:
void paintEvent(QPaintEvent *event) override;
};
- 在你的代码中创建 CallReminderDialog 实例,并显示出来:
CallReminderDialog dialog;
dialog.exec();
这样,你就可以在 paintEvent 方法中使用 QPainter 绘制 QDialog 的背景颜色和边框圆角效果了。
原文地址: https://www.cveoy.top/t/topic/p96R 著作权归作者所有。请勿转载和采集!