Unable to view image of SVG format in Android



  • Hi,

    I am using Android device to view the sample Apps, which is developed in Qt , version 5.7.
    I am able to view image of .png and .jpeg format , but unable to view .svg format. And i am adding the image of respective format to QLabel.

    And the image of svg format is not displaying.

    sample code :
    QFile file("./jpegFormat.svg"); // instead i give ./jpegFormat.jpeg or ./jpegFormat.png it will work
    file.setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner);
    file.open(QFile::ReadWrite);
    file.write(responseData);
    file.close();

    QPixmap pixmap("./jpegFormat.svg"); // instead i give ./jpegFormat.jpeg or ./jpegFormat.png it will work
    
    m_imageLabel->setPixmap(pixmap);
    

    Guidance is required.
    Thanks,


  • Qt Champions 2016

    Hi
    Do you have

    QT += svg

    http://doc.qt.io/qt-5/qtsvg-index.html

    Also , you can try the example
    http://doc.qt.io/qt-5/qtsvg-svgviewer-example.html



  • Hi! You cannot directly construct a pixmap from an SVG file. If you just want to show the SVG (as with QLabel for pixel graphics) use a QSvgWidget. If you want to rasterize the SVG by yourself then use QSvgRenderer to render the SVG via QPainter into a QImage or QPixmap.



  • Thanks for the solution guys @mrjj @Wieland
    It worked.

    Here is the sample code.

    QFile file("./svgFormat.svg");
    file.open(QFile::ReadWrite);
    file.write(responseData);
    file.close();
    
    QSvgRenderer renderer(QString("./svgFormat.svg"));
    
    QImage image(500, 200, QImage::Format_ARGB32);
    image.fill(0xaaA08080);  
    
    QPainter painter(&image);
    renderer.render(&painter);
    
    image.save("./svgFormat.png");
    
    QPixmap pixmap("./svgFormat.png");
    m_imageLabel->setPixmap(pixmap);
    

    I am saving in .png format after that i am again reading the saved .png format and adding to QLabel.



  • @Pradeep-Kumar Great to hear that it works now. Please mark the thread as solved :)



  • Yup i will mark as solved, so it will be useful.
    Thanks & Cheers.



  • If we want to save in .svg format

    here is the below code, this also works fine

    QByteArray responseData = response->readAll();
    
    QFile file("./svgFormat.svg");
    file.open(QFile::ReadWrite);
    file.write(responseData);
    file.close();
    
    QPixmap pixmap("./svgFormat.svg");
    m_imageLabel->setPixmap(pixmap);
    

    Another one which i posted earlier .

    QFile file("./svgFormat.svg");
    file.open(QFile::ReadWrite);
    file.write(responseData);
    file.close();

    QSvgRenderer renderer(QString("./svgFormat.svg"));

    QImage image(500, 200, QImage::Format_ARGB32);
    image.fill(0xaaA08080);

    QPainter painter(&image);
    renderer.render(&painter);

    image.save("./svgFormat.png");

    QPixmap pixmap("./svgFormat.png");
    m_imageLabel->setPixmap(pixmap);

    I am saving in .png format after that i am again reading the saved .png format and adding to QLabel. Both the code works.



  • I just posted because i was again working on the same app. I made changes and observed the code which posted previous also was working.

    So taught of posting once again , so it will be nice to share the contents.

    As @mrjj said we need to include Qt += svg in pro file.


Log in to reply
 

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