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. SVG file in QGraphicsView
Forum Updated to NodeBB v4.3 + New Features

SVG file in QGraphicsView

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 1.5k 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.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    Hi, I'm wondering if I can put an SVG file into a QGraphicsView widget. I've seen something about QSvgRenderer, but I don't know if that would work.
    I was using QPixmap when I was displaying jpg files, but svg files don't work.

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

      Hi,

      QGraphicsSvgItem is what you are looking for.

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

      S 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        QGraphicsSvgItem is what you are looking for.

        S Offline
        S Offline
        Sucharek
        wrote on last edited by
        #3

        Hi @SGaist, ok, but I don't know how to properly use it. I did this:

        QGraphicsSvgItem svg(":/hands/handLeftDark.svg");
        

        But I don't know how to add in in QGraphicsView now. I tried:

        ui->graphicsView_LeftHand->scene()->addItem(svg);
        

        But I don't know how to convert QGraphicsSvgItem to QGraphicsItem.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4
          QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(":/hands/handLeftDark.svg");
          ui->graphicsView_LeftHand->scene()->addItem(svgItem);
          

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

          S 1 Reply Last reply
          0
          • SGaistS SGaist
            QGraphicsSvgItem *svgItem = new QGraphicsSvgItem(":/hands/handLeftDark.svg");
            ui->graphicsView_LeftHand->scene()->addItem(svgItem);
            
            S Offline
            S Offline
            Sucharek
            wrote on last edited by
            #5

            @SGaist I'm getting an error:

            undefined reference to `QGraphicsSvgItem::QGraphicsSvgItem(QString const&, QGraphicsItem*)'
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Are you linking the svg module ?

              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
              0
              • JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                in pro
                QT += svg
                in cmake
                find_package(Qt5 REQUIRED ... Svg ... )

                S 1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  in pro
                  QT += svg
                  in cmake
                  find_package(Qt5 REQUIRED ... Svg ... )

                  S Offline
                  S Offline
                  Sucharek
                  wrote on last edited by
                  #8

                  Hi @JoeCFD, I added svg in .pro file, but I don't know is in cmake

                  JoeCFDJ 1 Reply Last reply
                  0
                  • S Sucharek

                    Hi @JoeCFD, I added svg in .pro file, but I don't know is in cmake

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #9

                    @Sucharek that may be good enough for you after you put it into pro file. I use both.

                    S 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @Sucharek that may be good enough for you after you put it into pro file. I use both.

                      S Offline
                      S Offline
                      Sucharek
                      wrote on last edited by
                      #10

                      Hi @JoeCFD, ok I just realised that I put it in a wrong thing in my .pro file, and when I put it right, it works. I don't know where the cmake file is, but it doesn't matter not because it works.
                      But I have another problem, you see, I have a dark and light mode in my app, and when I switch, QGraphicsView makes 2 lines on the edges of the image. I tried "ui->graphicsview->scene()->clear", but that just crashes the application. This is how the lines look:
                      716657ac-e02a-4eef-affe-60023d88ad2b-image.png
                      33f10802-f9d5-4e0d-a70e-07582c93f2a3-image.png

                      1 Reply Last reply
                      0
                      • JoeCFDJ Offline
                        JoeCFDJ Offline
                        JoeCFD
                        wrote on last edited by
                        #11

                        override this one and define the size of your pic
                        QRectF boundingRect() const

                        S 1 Reply Last reply
                        0
                        • JoeCFDJ JoeCFD

                          override this one and define the size of your pic
                          QRectF boundingRect() const

                          S Offline
                          S Offline
                          Sucharek
                          wrote on last edited by
                          #12

                          @JoeCFD, ok I tried that, but it doesn't seem to be working...
                          It doesn't display anything. Here's what it displays: 93648bc8-6ccf-491a-95f2-73cbca12e548-image.png
                          My code:

                          QPixmap left(":/hands/handLeftDark.svg");
                          QRectF size(150, 150, 150, 150);
                          ui->graphicsView_LeftHand->scene()->addPixmap(left);
                          ui->graphicsView_LeftHand->setSceneRect(size);
                          
                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Sucharek
                            wrote on last edited by
                            #13

                            Hi, so I solved it by doing "ui->graphicsView_LeftHand->scene()->removeItem(svgLeftDark);" and then adding the other svg.
                            So it's like this:

                            QGraphicsScene *sceneLeft = new QGraphicsScene(this);
                            ui->graphicsView_LeftHand->setScene(sceneLeft);
                            
                            QGraphicsSvgItem *add = new QGraphicsSvgItem("path/to/svg.svg");
                            QGraphicsSvgItem *remove = new QGraphicsSvgItem("path/to/svg.svg");
                            
                            ui->graphicsView_LeftHand->scene()->removeItem(remove); //remove the svg that you don't want
                            ui->graphicsView_LeftHand->scene()->addItem(add); //add the svg that you want
                            

                            If you want to have it across different voids, just add this at the top of the .cpp file (where you declare cross void variables, ...):

                            QGraphicsSvgItem *add = new QGraphicsSvgItem("path/to/svg.svg");
                            QGraphicsSvgItem *remove = new QGraphicsSvgItem("path/to/svg.svg");
                            
                            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