抱歉,看起来有些混淆。在 QPaintDevice
类中确实没有直接的 size()
方法。
如果您想获取当前绘图设备的大小,可以使用 QPainter
的 device()
方法返回的指针调用 width()
和 height()
方法来获取宽度和高度。请尝试以下修改:
if (m_drawing) {
painter.setPen(m_pen);
painter.fillRect(rect(), m_Color);
// 绘制历史路径
for (int i = 0; i <= currentPathIndex; ++i) {
const QPainterPath& path = paths[i];
const QPen& pen = pathPens[i];
painter.setPen(pen);
painter.drawPath(path);
}
} else {
// 创建一个临时的绘图设备
QImage tempImage(painter.device()->width(), painter.device()->height(), QImage::Format_ARGB32_Premultiplied);
QPainter tempPainter(&tempImage);
// 将历史路径绘制到临时设备上
for (int i = 0; i <= currentPathIndex; ++i) {
const QPainterPath& path = paths[i];
const QPen& pen = pathPens[i];
tempPainter.setPen(pen);
tempPainter.drawPath(path);
}
// 擦除当前路径
const QPainterPath& erasePath = paths[currentPathIndex];
const QPen& erasePen = pathPens[currentPathIndex];
tempPainter.setCompositionMode(QPainter::CompositionMode_Clear);
tempPainter.setPen(erasePen);
tempPainter.drawPath(erasePath);
// 将临时设备的内容绘制到主设备上,只有擦除部分会显示透明色
painter.drawImage(QPoint(), tempImage);
}
这样应该可以解决错误,并创建正确大小的临时绘图设备。如果还有其他问题,请随时提问。
内容由零声教学AI助手提供,问题来源于学员提问