qt - 如何将 QGraphicsView 中的图像保存为 bmp/jpg

标签 qt qt4

我是 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/

相关文章:

qt - 如何将任何类型的文件读取为二进制值?

c++ - cmake:如何使用静态链接的 boost (或嵌入 boost )创建库

c++ - QProcess:进程仍在运行时被销毁

html - HTML 表格中的双线

c++ - Qt 前向声明产生错误

qt4 - 有没有办法在 QObject 中的属性更改时收到通知?

python - 如何在 Windows 10 中安装 PyQt4 for Python 3.6

c++ - QT中如何从json中加载base64图片数据

python - PyQt 中的复杂模型项

excel - 在 Qt 中格式化 Excel 文档