Files
EJM_Display/PublicFunctions/BrushPad.cpp

49 lines
1.2 KiB
C++
Raw Permalink Normal View History

2025-09-15 22:28:43 +08:00
#include "BrushPad.h"
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>
BrushPad::BrushPad(QWidget *parent)
: QGroupBox(parent)
{
setMinimumSize(100, 100);
m_canvas = QPixmap(size());
m_canvas.fill(Qt::transparent);
}
void BrushPad::setBrushImage(const QImage &img)
{
m_brush = img.convertToFormat(QImage::Format_ARGB32_Premultiplied);
}
void BrushPad::moveBrush(const QPoint &globalPos)
{
// 1. 不管笔刷图,直接画一个 20×20 的红色方块到指定坐标
QPainter p(&m_canvas);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.drawImage(globalPos, m_brush); // 直接叠加,不清屏
update();
}
void BrushPad::paintEvent(QPaintEvent *)
{
// 让基类画边框/标题
QGroupBox::paintEvent(nullptr);
// 直接把我们自己的画布贴出来
QPainter p(this);
p.drawPixmap(contentsRect().topLeft(), m_canvas);
// 这里 **绝对不要再调用 update()/repaint()**
}
void BrushPad::resizeEvent(QResizeEvent *)
{
// 画布跟随控件大小
QPixmap newPix(contentsRect().size());
newPix.fill(Qt::transparent);
QPainter p(&newPix);
p.drawPixmap(0, 0, m_canvas);
m_canvas = newPix;
}