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. Show scaled svg
Forum Updated to NodeBB v4.3 + New Features

Show scaled svg

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.1k Views 1 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.
  • J Offline
    J Offline
    jss193
    wrote on last edited by jss193
    #1

    Hello,

    I am trying to display a svg image on my screen, but it does not appear scaled ( I just can see a piece of the image), I mean it is not scaled, I tried the following code:

        QImage image;
        image.load("pathto.svg");
    
        QPainter painter(this);
        painter.drawImage(QPoint(10,10),image);
    

    I tried the following too:

        QImage image(QString("pathto.svg"));
        
        QPainter painter(&image);
        painter.drawImage(QPoint(10,10),image);
    
    

    In first case the image is not resized and I dont know how to do it, can someone help me?

    In the second case the image does not even appear if I dont use this pointer, I dont well understand why, could you explain me why using this instead of &image??

    Thannk you!

    J.HilkJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not just load the svg in a QLabel ?

      In the second case, you are painting image on itself.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Why not just load the svg in a QLabel ?

        In the second case, you are painting image on itself.

        J Offline
        J Offline
        jss193
        wrote on last edited by
        #3

        @SGaist
        Hi, I also tried but it shows the same, the image is partially showed, what I wish is to (for instance) show svg image scaled to qlabel, what is, shown completely and scaled to qlabel size.

        Thank u

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Isn't the scaledContents property what you want ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • J jss193

            Hello,

            I am trying to display a svg image on my screen, but it does not appear scaled ( I just can see a piece of the image), I mean it is not scaled, I tried the following code:

                QImage image;
                image.load("pathto.svg");
            
                QPainter painter(this);
                painter.drawImage(QPoint(10,10),image);
            

            I tried the following too:

                QImage image(QString("pathto.svg"));
                
                QPainter painter(&image);
                painter.drawImage(QPoint(10,10),image);
            
            

            In first case the image is not resized and I dont know how to do it, can someone help me?

            In the second case the image does not even appear if I dont use this pointer, I dont well understand why, could you explain me why using this instead of &image??

            Thannk you!

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @jss193

            you need to tell QPainter a size, so that it knows what to size the image to. Take a look at the function parameters

            void QPainter::drawImage(const QRectF & target, const QImage & image, const QRectF & source, Qt::ImageConversionFlags flags = Qt::AutoColor)
            

            target = the rectangle to be drawn into
            image = the image to be drawn
            source= the target rectangle of the image, in case you only want part of it

            the function you're using is

            void QPainter::drawImage(const QPoint & point, const QImage & image)
            

            that completely ignores any size adjustments :)


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            3
            • BuckwheatB Offline
              BuckwheatB Offline
              Buckwheat
              wrote on last edited by
              #6

              Hi @jss193

              As @SGaist stated, you can always put the SVG item into a label or a button as its image.

              Have you tried QSvgRenderer instead of loading the svg file into an image? You will need to add QT += svg to your PRO file and include <QSvgRenderer> in your file.

              // Setup the painter to the QWidget (or use an image instead of a widget)
              QPainter painter (this);
              // Create the SVG object
              QSvgRenderer renderer ("file.svg");
              // Set the viewing box
              renderer.setViewBox (painter.viewport ());
              // Now set the scale or other transforms into the painter
              ...
              // Render the image
              renderer.render (&painter);

              The problem with going into an image is that it will create either a MEGA huge image or a small image or a normal image based on the SVG document size. Then, scaling this image you lose all the nice smoothing and crispness using an SVG gives you. It is best to scale the SVG to the size you need and not the other way around.

              I actually load an SVG into an image like you are doing and use it as a background for painting. This is actually faster than drawing the SVG each time (for my case) and the image looks crisp since the SVG is scaled and not the image.

              Enjoy!

              Dave Fileccia

              1 Reply Last reply
              2

              • Login

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