我是 Qt 的新手。 问题是:将图像转换为 QGraphicsView 后,我使用 qrubberband 选择图像的裁剪区域。目前选择裁剪区域是成功的。但是我不知道之后如何将该裁剪区域保存为jpg/bmp。请注意,我为 GraphicsView 制作了一个名为 CGraphicsView 的 ui 组件。
void CGraphicsView::mousePressEvent
( QMouseEvent* event)
{
mypoint = event->pos();
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);//new rectangle band
rubberBand->setGeometry(QRect(mypoint, QSize()));
rubberBand->show();
}
void CGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if (rubberBand)
{
rubberBand->setGeometry(QRect(mypoint, event->pos()).normalized());//Area Bounding
}
}
void CGraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
if (rubberBand)
{
QRect myRect(mypoint, event->pos());
rubberBand->hide();// hide on mouse Release
QImage copyImage; //<= this Qimage hold nothing
copyImage = copyImage.copy(myRect);
}
}
最佳答案
Qt中有一个特殊的方法。它允许获取 View 的屏幕截图。
QString fileName = "path";
QPixmap pixMap = QPixmap::grabWidget(graphicsView, rectRegion);
pixMap.save(fileName);
Save()方法可以将图片保存为不同格式并进行压缩。
还可以使用grabWidget()方法来获取另一个小部件。此外,该方法以 QRect 作为参数,因此您可以创建所需区域的屏幕截图。
关于qt - 如何将 QGraphicsView 中的图像保存为 bmp/jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25717448/