在 QImage 上绘制 QPainterPath 的指定部分 [缩放和平移]
Drawing specified part of QPainterPath on QImage [zoom and pan]
即如果我从 Qt5 tutorial 中指定的示例中指定一些立方线:
QPainterPath path;
path.addRect(20, 20, 60, 60);
path.moveTo(0, 0);
path.cubicTo(99, 0, 50, 50, 99, 99);
path.cubicTo(0, 99, 50, 50, 0, 0);
QPainter painter(this);
painter.fillRect(0, 0, 100, 100, Qt::white);
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine,
Qt::FlatCap, Qt::MiterJoin));
painter.setBrush(QColor(122, 163, 39));
painter.drawPath(path);
这组曲线构成
现在我只想渲染 QImage
上由起点=[20px,50px]、宽度=80px 和高度=50px 的某个区域指定的曲线的一部分,因此结果看起来像这个:
或者,如果可能的话,使用 3 倍缩放进行渲染,因此结果 QImage
看起来是一样的,但大小 =[240px,150px]
我是 Qt 的新手,有人可以给我看一个有效的代码示例吗?
我有一个代码示例供您使用。但找出方法并不难。你只需要阅读文档,一切都很容易找到。 Qt的文档真的很棒。
QApplication a(argc, argv);
QImage img(100, 100, QImage::Format_ARGB32);
QPainterPath path;
path.addRect(20, 20, 60, 60);
path.moveTo(0, 0);
path.cubicTo(99, 0, 50, 50, 99, 99);
path.cubicTo(0, 99, 50, 50, 0, 0);
QPainter painter(&img);
painter.fillRect(0, 0, 100, 100, Qt::white);
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine,
Qt::FlatCap, Qt::MiterJoin));
painter.setBrush(QColor(122, 163, 39));
painter.drawPath(path);
painter.end();
QPixmap pixmap( QPixmap::fromImage(img).copy(20, 50, 80, 50).scaled(240,150) );
// option 1, use a QLabel ( only for simple cases )
QLabel label;
label.setPixmap( pixmap );
label.show();
// option 2, use a QGraphicsScene ( far more flexible )
QGraphicsView view;
QGraphicsScene scene;
scene.addPixmap( pixmap );
scene.setSceneRect( img.rect() );
view.setScene(&scene);
view.show();
return a.exec();
您可以变换画家坐标系:
QPainter painter(this);
painter.scale(3, 3); // zoom 3 times
painter.translate(-20, -50); // offset origin to 20x50
// ... render stuff
这比其他答案有优势,因为它会像您提供更大的坐标一样进行渲染,而不是将其渲染得较小然后放大光栅图像,这会降低图像质量。此外,Qt 可能会将其优化为不在图像外部渲染,因此渲染较少,并且您不需要裁剪和丢弃结果。
结果:
将其与放大的栅格进行比较:
即如果我从 Qt5 tutorial 中指定的示例中指定一些立方线:
QPainterPath path;
path.addRect(20, 20, 60, 60);
path.moveTo(0, 0);
path.cubicTo(99, 0, 50, 50, 99, 99);
path.cubicTo(0, 99, 50, 50, 0, 0);
QPainter painter(this);
painter.fillRect(0, 0, 100, 100, Qt::white);
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine,
Qt::FlatCap, Qt::MiterJoin));
painter.setBrush(QColor(122, 163, 39));
painter.drawPath(path);
这组曲线构成
现在我只想渲染 QImage
上由起点=[20px,50px]、宽度=80px 和高度=50px 的某个区域指定的曲线的一部分,因此结果看起来像这个:
或者,如果可能的话,使用 3 倍缩放进行渲染,因此结果 QImage
看起来是一样的,但大小 =[240px,150px]
我是 Qt 的新手,有人可以给我看一个有效的代码示例吗?
我有一个代码示例供您使用。但找出方法并不难。你只需要阅读文档,一切都很容易找到。 Qt的文档真的很棒。
QApplication a(argc, argv);
QImage img(100, 100, QImage::Format_ARGB32);
QPainterPath path;
path.addRect(20, 20, 60, 60);
path.moveTo(0, 0);
path.cubicTo(99, 0, 50, 50, 99, 99);
path.cubicTo(0, 99, 50, 50, 0, 0);
QPainter painter(&img);
painter.fillRect(0, 0, 100, 100, Qt::white);
painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine,
Qt::FlatCap, Qt::MiterJoin));
painter.setBrush(QColor(122, 163, 39));
painter.drawPath(path);
painter.end();
QPixmap pixmap( QPixmap::fromImage(img).copy(20, 50, 80, 50).scaled(240,150) );
// option 1, use a QLabel ( only for simple cases )
QLabel label;
label.setPixmap( pixmap );
label.show();
// option 2, use a QGraphicsScene ( far more flexible )
QGraphicsView view;
QGraphicsScene scene;
scene.addPixmap( pixmap );
scene.setSceneRect( img.rect() );
view.setScene(&scene);
view.show();
return a.exec();
您可以变换画家坐标系:
QPainter painter(this);
painter.scale(3, 3); // zoom 3 times
painter.translate(-20, -50); // offset origin to 20x50
// ... render stuff
这比其他答案有优势,因为它会像您提供更大的坐标一样进行渲染,而不是将其渲染得较小然后放大光栅图像,这会降低图像质量。此外,Qt 可能会将其优化为不在图像外部渲染,因此渲染较少,并且您不需要裁剪和丢弃结果。
结果:
将其与放大的栅格进行比较: