Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unable to view image of SVG format in Android
Forum Update on Monday, May 27th 2025

Unable to view image of SVG format in Android

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Pradeep KumarP Offline
    Pradeep KumarP Offline
    Pradeep Kumar
    wrote on last edited by A Former User
    #1

    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,

    Pradeep Kumar
    Qt,QML Developer

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      3
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        5
        • Pradeep KumarP Offline
          Pradeep KumarP Offline
          Pradeep Kumar
          wrote on last edited by Pradeep Kumar
          #4

          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
          Qt,QML Developer

          ? 1 Reply Last reply
          1
          • Pradeep KumarP Pradeep Kumar

            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.

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

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

            1 Reply Last reply
            2
            • Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #6

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

              Pradeep Kumar
              Qt,QML Developer

              1 Reply Last reply
              1
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by Pradeep Kumar
                #7

                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.

                Pradeep Kumar
                Qt,QML Developer

                1 Reply Last reply
                2
                • Pradeep KumarP Offline
                  Pradeep KumarP Offline
                  Pradeep Kumar
                  wrote on last edited by Pradeep Kumar
                  #8

                  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.

                  Pradeep Kumar
                  Qt,QML Developer

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved