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. Bad Svg in QGraphicsView [Qt 5.5]
Forum Updated to NodeBB v4.3 + New Features

Bad Svg in QGraphicsView [Qt 5.5]

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.6k Views 2 Watching
  • 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.
  • GGAllinG Offline
    GGAllinG Offline
    GGAllin
    wrote on last edited by
    #1

    Hello everyone
    I've a problem with some svg icons and retina monitor..
    they looks perfect when are scaled at 100% but if are under 100% they looks really bad
    here's my code

    #include <QApplication>
    #include <QDebug>
    #include <QWidget>
    #include <QGraphicsView>
    #include <QGraphicsSvgItem>
    #include <QSvgRenderer>
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        QGraphicsScene scene;
        QGraphicsView view(&scene);
    
        QString pic(argv[1]);
        QSvgRenderer renderer(pic);
    
        int w = 58;
        int h = 58;
        int hdpi = 2;
        QImage image(w*hdpi, h*hdpi, QImage::Format_ARGB32);
        QPainter painter(&image);
        renderer.render(&painter);
    
        QGraphicsPixmapItem pix( QPixmap::fromImage(image));
        //pix.setScale(1.0/hdpi); // 100%
        pix.setScale(0.4/hdpi);
        scene.addItem(&pix);
    
        view.show();
        return app.exec();
    }
    
    

    here the result
    link text

    and at 100%
    link text

    are there something wrong in my code ?
    Help me please! ;-)

    Tnks.
    GG

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

      hi
      have you tried with
      painter.setRenderHint(QPainter::Antialiasing);
      or
      painter.setRenderHint(QPainter::HighQualityAntialiasing);
      to see if u can get it to smooth it ?

      GGAllinG 1 Reply Last reply
      0
      • GGAllinG GGAllin

        Hello everyone
        I've a problem with some svg icons and retina monitor..
        they looks perfect when are scaled at 100% but if are under 100% they looks really bad
        here's my code

        #include <QApplication>
        #include <QDebug>
        #include <QWidget>
        #include <QGraphicsView>
        #include <QGraphicsSvgItem>
        #include <QSvgRenderer>
        
        int main(int argc, char **argv)
        {
            QApplication app(argc, argv);
        
            QGraphicsScene scene;
            QGraphicsView view(&scene);
        
            QString pic(argv[1]);
            QSvgRenderer renderer(pic);
        
            int w = 58;
            int h = 58;
            int hdpi = 2;
            QImage image(w*hdpi, h*hdpi, QImage::Format_ARGB32);
            QPainter painter(&image);
            renderer.render(&painter);
        
            QGraphicsPixmapItem pix( QPixmap::fromImage(image));
            //pix.setScale(1.0/hdpi); // 100%
            pix.setScale(0.4/hdpi);
            scene.addItem(&pix);
        
            view.show();
            return app.exec();
        }
        
        

        here the result
        link text

        and at 100%
        link text

        are there something wrong in my code ?
        Help me please! ;-)

        Tnks.
        GG

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @GGAllin
        You're scaling a raster image, so don't do that. Instead provide a smaller image for the QSvgRenderer to render onto. For example, what's wrong with this?

            QImage image(0.4 * w, 0.4* h, QImage::Format_ARGB32);
            QPainter painter(&image);
            renderer.render(&painter);
        
            QGraphicsPixmapItem pix( QPixmap::fromImage(image));
            scene.addItem(&pix);
        

        The idea is that the last thing you do is render the vector data to raster, so you can actually keep the quality.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • mrjjM mrjj

          hi
          have you tried with
          painter.setRenderHint(QPainter::Antialiasing);
          or
          painter.setRenderHint(QPainter::HighQualityAntialiasing);
          to see if u can get it to smooth it ?

          GGAllinG Offline
          GGAllinG Offline
          GGAllin
          wrote on last edited by
          #4

          @mrjj

          @mrjj said:

          hi
          have you tried with
          painter.setRenderHint(QPainter::Antialiasing);
          or
          painter.setRenderHint(QPainter::HighQualityAntialiasing);
          to see if u can get it to smooth it ?

          @kshegunov said:

          @GGAllin
          You're scaling a raster image, so don't do that. Instead provide a smaller image for the QSvgRenderer to render onto. For example, what's wrong with this?

              QImage image(0.4 * w, 0.4* h, QImage::Format_ARGB32);
              QPainter painter(&image);
              renderer.render(&painter);
          
              QGraphicsPixmapItem pix( QPixmap::fromImage(image));
              scene.addItem(&pix);
          

          The idea is that the last thing you do is render the vector data to raster, so you can actually keep the quality.

          @mrjj said:

          hi
          have you tried with
          painter.setRenderHint(QPainter::Antialiasing);
          or
          painter.setRenderHint(QPainter::HighQualityAntialiasing);
          to see if u can get it to smooth it ?

          Nothing change with QPainter::HighQualityAntialiasing or QPainter::Antialiasing

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

            ok.
            good to know.
            Was just a shot.

            so @kshegunov way should do it :)

            1 Reply Last reply
            0
            • GGAllinG Offline
              GGAllinG Offline
              GGAllin
              wrote on last edited by
              #6

              Yeah!```
              QImage image(0.4 * w, 0.4* h, QImage::Format_ARGB32);
              QPainter painter(&image);
              renderer.render(&painter);

              QGraphicsPixmapItem pix( QPixmap::fromImage(image));
              scene.addItem(&pix);
              
              
              works perfect!
              Many thanks to all!!
              1 Reply Last reply
              0
              • GGAllinG Offline
                GGAllinG Offline
                GGAllin
                wrote on last edited by
                #7

                better result using fill(Qt::Transparent)

                    int w = svgSize.width();
                    int h = svgSize.height();
                    int hdpi = docView->devicePixelRatio() <2 ? 1: 2;
                    QSvgRenderer renderer("icon.svg");
                    QImage image(scaleF*w*hdpi,scaleF* h*hdpi, QImage::Format_ARGB32);
                    image.fill(Qt::TransparentMode);
                    QPainter painter(&image);
                    painter.setRenderHint(QPainter::HighQualityAntialiasing);
                    renderer.render(&painter);
                    QGraphicsPixmapItem* pix =new QGraphicsPixmapItem(QPixmap::fromImage(image));
                    pix->setScale(1.0/hdpi);
                
                kshegunovK 1 Reply Last reply
                0
                • GGAllinG GGAllin

                  better result using fill(Qt::Transparent)

                      int w = svgSize.width();
                      int h = svgSize.height();
                      int hdpi = docView->devicePixelRatio() <2 ? 1: 2;
                      QSvgRenderer renderer("icon.svg");
                      QImage image(scaleF*w*hdpi,scaleF* h*hdpi, QImage::Format_ARGB32);
                      image.fill(Qt::TransparentMode);
                      QPainter painter(&image);
                      painter.setRenderHint(QPainter::HighQualityAntialiasing);
                      renderer.render(&painter);
                      QGraphicsPixmapItem* pix =new QGraphicsPixmapItem(QPixmap::fromImage(image));
                      pix->setScale(1.0/hdpi);
                  
                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @GGAllin
                  Forgive my ignorance, but how is this:

                  QImage image(scaleF * w * hdpi, scaleF * h * hdpi, QImage::Format_ARGB32);
                  QGraphicsPixmapItem* pix = new QGraphicsPixmapItem(QPixmap::fromImage(image));
                  pix->setScale(1.0 / hdpi);
                  

                  different from this:

                  QImage image(scaleF * w, scaleF * h, QImage::Format_ARGB32);
                  QGraphicsPixmapItem* pix = new QGraphicsPixmapItem(QPixmap::fromImage(image));
                  

                  The whole point of using vector graphics is not to scale raster images, but to render directly to the correct sizes. isn't it? So why do you insist on rendering a larger image and then scaling it down?

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0

                  • Login

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