Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to show QGraphicsScene background and border but do not save into image ?



  • And why QGraphicsScene background fill full of QGraphicsView ?

    scene->setBackgroundBrush(pixmap);
    
    void MainWindow::open(QString filepath)
    {
        scene->clear();
        QPixmap pixmap(filepath);
        QGraphicsPixmapItem *GPI = scene->addPixmap(pixmap);
        GPI->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);  
        scene->setSceneRect(0, 0, pixmap.width(), pixmap.height());
    }
    
    void MainWindow::save(QString filepath)
    {
        QSize size(scene->width(), scene->height());
        QImage image(size, QImage::Format_ARGB32);
        QPainter painter(&image);
        scene->render(&painter);
        image.save(filepath);
    }
    

  • Moderators

    @sonichy
    try painting the background pixmap in QGraphicsView::drawBackground(), this way it shouldn't be taken into account by QGraphicsScene::render()



  • @raven-worx OK!

    ui->graphicsView->setBackgroundBrush(pixmap);
    

Log in to reply