xuqiang 5 месяцев назад
Родитель
Сommit
5270b65c18
2 измененных файлов с 51 добавлено и 0 удалено
  1. 20 0
      include/canvas.h
  2. 31 0
      src/canvas.cpp

+ 20 - 0
include/canvas.h

@@ -0,0 +1,20 @@
+#include <QWidget>
+
+class Canvas : public QWidget
+{
+    Q_OBJECT
+
+public:
+    explicit Canvas( QWidget *parent = nullptr);
+    ~Canvas();
+
+    const QColor &backgroundColor() const;
+    void setBackgroundColor(const QColor &newBackgroundColor);
+
+protected:
+    void paintEvent(QPaintEvent *event) override;
+
+private:
+    QColor m_backgroundColor;
+
+};

+ 31 - 0
src/canvas.cpp

@@ -0,0 +1,31 @@
+#include "canvas.h"
+#include <QPainter>
+
+Canvas::Canvas(QWidget *parent)
+    : QWidget{parent}
+    , m_backgroundColor(Qt::black)
+{
+
+}
+
+Canvas::~Canvas()
+{
+
+}
+
+const QColor &Canvas::backgroundColor() const
+{
+    return m_backgroundColor;
+}
+
+void Canvas::setBackgroundColor(const QColor &newBackgroundColor)
+{
+    m_backgroundColor = newBackgroundColor;
+}
+
+void Canvas::paintEvent(QPaintEvent *event)
+{
+    // 绘制背景颜色
+    QPainter painter(this);
+    painter.fillRect(rect(), m_backgroundColor);
+}