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);
    pixMap.save(fileName);
    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;
        rect->setRect(QRectF(0,0,100,60));
        QGraphicsScene *scene = new QGraphicsScene(this);
        ui->graphicsView->setScene(scene);
        scene->addItem(rect);
    
        const QPixmap pixmap = ui->graphicsView->grab();
        const QString fileName("/home/pw/image.png");
        qDebug() << pixmap.save(fileName);
    


  • @Wieland I think you forgot pixmap.save(fileName,"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 pixmap.save("/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: pixmap.save("/home/pw/myfile", "JPG") (see supported file formats).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.