我们知道,在 qt 中绘制图像时,会使用 qpainter。最近,我使用 drawLine() 函数来绘制用户正在涂鸦的任何内容。这是通过将来自 mouseMoveEvent 的 lastPoint 和 currentPoint 传递给实现 drawLine() 的自定义函数来完成的。我已经传递了该自定义函数的参数,如下所示:
void myPaint::mouseMoveEvent(QMouseEvent *event) {
qDebug() << event->pos();
if ((event->buttons() & Qt::LeftButton) && scribbling) {
pixelList.append(event->pos());
drawLineTo(event->pos());
lastPoint = event->pos();
}
}
现在在 qDebug() 的帮助下,我注意到绘制时遗漏了一些像素,但绘制是精确的。我查看了 qt-painting 的来源,我看到 drawLine() 正在调用 drawLines(),它使用 qpainterPath 在图像上绘制形状。
我的问题是,是否有任何方法可以跟踪这些“丢失”的像素或找到所有已绘制的像素的方法?
谢谢!
void myPaint::drawLineTo(const QPoint &endPoint) {
QPainter painter(image); //image is initialized in the constructor of myPaint
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(Qt::blue, myPenWidth, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
painter.drawLine(lastPoint, endPoint);
modified = true;
lastPoint = endPoint; //at the mousePressEvent, the event->pos() will be stored as
// lastPoint
update();
}