[SOLVED]QGraphicsView blurs an svg image on scaling (zooming)



  • When I scale(zoom) a svg image it blurs:
    @ui->graphicsView->scale(2,2);@

    The same image when zoomed with any image viewer, does not blur.



  • I also tried scaling the QImage object instead of graphicsview but still it blurs



  • This is because this function scales your whole view, not the item. Try with setScale of your QGraphicsSvgItem or try with the QTransform (void QGraphicsItem::setTransform ( const QTransform & matrix, bool combine = false ) function).



  • Thanks for your reply. Actually I haven't usedQGraphicsSvgItem. My code is below:
    @void aViewer::on_actionOpen_triggered()
    {
    /******** Opening file dialog for selecting image to be displayed **********/
    fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());

    fileInfo.setFile(fileName);                       // Storing information of image file selected
    
        if (!fileName.isEmpty())                      // if file exists
        {
            image.load(fileName);                     // loading image in to a QImage object
    
            if (image.isNull())                       // if image does not exist display error and return
            {
                QMessageBox::information(this, tr("aViewer"),tr("Cannot load %1.").arg(fileName));
                return;
            }
    
            tempImage=image;
    
            displayImage.clear();
            /******** if image exists display image *********************/
            displayImage.addPixmap(QPixmap::fromImage(image));                // add image to QGraphicsScene object
            displayImage.setSceneRect(0, 0, image.width(), image.height());   // make sure it is centered
    
            /****** ensure that full image is visible, i.e. scaled to viewing port *********/
            if(image.height()>ui->graphicsView->height()||image.width()>ui->graphicsView->width())
            {
                ui->graphicsView->fitInView(displayImage.sceneRect(), Qt::KeepAspectRatio);
            }
            else
            {
                ui->graphicsView->setTransform(QTransform());                 // make sure it is displayed at original size
            }
    
            /******* Displaying filename as window title *******************/
            setWindowTitle(fileInfo.fileName());
    
            /******* Update Status Bar ******************/
            displayStatus();
    
        }
    

    }

    void aViewer::on_actionZoom_In_triggered()
    {
    ui->graphicsView->scale(1.1,1.1);
    }@

    Where should i make the changes!



  • My suggestion is:

    Update the .pro file: QT += core gui svg
    then, take advantage of the QGraphicsScene/View api:

    @
    #include <QGraphicsSvgItem>

    void aViewer::on_actionOpen_triggered()
    {
    //you can setSceneRect here to fit the QGraphicsView size
    //I assume that you have the QGraphicsScene *scene;
    //ui->graphiscView->setScene( scene );

    fileName = QFileDialog::getOpenFileName(this,tr("Open File"),
                                            QDir::currentPath(), "SVG (*.svg)" );
    fileInfo.setFile&#40;fileName&#41;;
    if (!fileName.isEmpty(&#41;&#41;
    {
        image.load(fileName&#41;;
    
        if (image.isNull())
        {
          QMessageBox::information(this, tr("aViewer"),tr("Cannot load %1.").arg(fileName));
          return;
        }
    
        tempImage=image;
        displayImage.clear();
        //displayImage.addPixmap(QPixmap::fromImage(image)); //we don't want this
    
        //my suggestion
        QGraphicsSvgItem *item = new QGraphicsSvgItem( fileName );
        scene->addItem( item );
        ui->graphicsView->fitInView(item, Qt::KeepAspectRatio);
        //or
        //item->scale( somevaluex, somevaluey)
    
        /******* Displaying filename as window title *******************/
        setWindowTitle(fileInfo.fileName());
    
        /******* Update Status Bar ******************/
        displayStatus();
    }
    

    }
    @

    I wrote this without any compiler so I might be mistaken somewhere, but I hope you got my idea.



  • Thanks a lot, it worked like a charm. I needed image (QImage) object too, for some other features, so i retained that also.
    @if(fileInfo.fileName()!=fileInfo.baseName()+".svg")
    {
    displayImage.addPixmap(QPixmap::fromImage(image)); // add image to QGraphicsScene object
    }
    else if(fileInfo.fileName()==fileInfo.baseName()+".svg")
    {
    QGraphicsSvgItem *svgItem = new QGraphicsSvgItem( fileName );
    displayImage.addItem( svgItem );
    }@

    @void aViewer::on_actionZoom_In_triggered()
    {
    ui->graphicsView->scale(1.1,1.1);
    }@


Log in to reply
 

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