Save Graphics View Scene

  • How I can save the scene, so it would be a .jpg file or a .gif file?
    QString fileName = "path";
    QPixmap pixMap = QPixmap::grabWidget(ui->graphicsView_animation, 100,100,10,10);;
    I have tried the code above but it didn't work

  • QPixmap::grabWidget is obsolete. Use QWidget::grab() instead.

  • @Wieland I can't make an img from Graphics View which is moving rectangles, it makes a blank img, why?

  • Works for me:

        QGraphicsRectItem *rect = new QGraphicsRectItem;
        QGraphicsScene *scene = new QGraphicsScene(this);
        const QPixmap pixmap = ui->graphicsView->grab();
        const QString fileName("/home/pw/image.png");
        qDebug() <<;

  • @Wieland I think you forgot,"jpg"). Thx it worked.

  • bool QPixmap::save(const QString & fileName, const char * format = 0, int quality = -1) const

    "If format is 0, an image format will be chosen from fileName's suffix." (see QPixmap documentation)

    So you can say"/home/pw/myfile.jpg") and it will automatically detect the ".jpg" suffix. If you want to save the image without a suffix you must specify what file format you want to use:"/home/pw/myfile", "JPG") (see supported file formats).

Log in to reply