我正在编写一个小型图形编辑器,需要一些帮助。 我正在这样绘制QImage:
void Editor::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// zoom is an int, representing zoomFactor from 1 to 12.
painter.drawImage(
QRect(0, 0, image.width() * zoom , image.height() * zoom),
image);
if (zoom >= 3 && showGrid) {
painter.setPen(palette().foreground().color());
painter.setPen(Qt::DotLine);
// this is how I draw grid
for (int i = 0; i <= image.width(); ++i)
painter.drawLine(zoom * i, 0,
zoom * i, zoom * image.height());
for (int j = 0; j <= image.height(); ++j)
painter.drawLine(0, zoom * j,
zoom * image.width(), zoom * j);
}
// (...)
}
它可以很好地处理像这样的图像(16 x 16)
![1](https://i.imgur.com/HVf6O.png)
更新:问题出乎意料地解决了。我注意到编辑器是QGLWidget,于是我尝试将其更改为QWidget,一切都正常工作了。我真是太蠢了-_- 顺便说一下,可能有更方便的方法来缩放图像吗?(例如裁剪不需要绘制的像素)