Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Display different QGraphicsSvgItems in QGraphicViews

    General and Desktop
    3
    9
    272
    Loading More Posts
    • 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.
    • G
      gewitt last edited by

      Hi,
      first of all, I am kinda new to C ++ and Qt stuff. Sorry if my Questions are a bit dumb ^^

      I created a Qt Widgets Application, and add some GraphicsViewItems to the mainwindow.ui via the Design Tab. Now I wanted to Display different Svgs in my GraphicsView Object.

      What I am doin is the Following:
      mainwondow.h File:

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include <QString>
      #include <QSvgRenderer>
      #include <QGraphicsSvgItem>
      
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      class QAction;
      class QGraphicsView;
      class QGraphicsScene;
      class QGraphicsRectItem;
      class QLabel;
      class QGraphicsSvgItem;
      class QSvgRenderer;
      
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          bool loadFile(const QString &path);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
          QGraphicsScene *scene;
          QGraphicsSvgItem *f1;
          QGraphicsSvgItem *f2;
          QGraphicsSvgItem *f3;
          QGraphicsSvgItem *f4;
          QGraphicsSvgItem *f5;
          QGraphicsSvgItem *f6;
      
      };
      #endif // MAINWINDOW_H
      
      

      mainwindow.cpp File:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          scene = new QGraphicsScene(this);
      
          QSvgRenderer *renderer = new QSvgRenderer(QStringLiteral("Icons.svg"));
      
      
          f1->setSharedRenderer(renderer);
          f1->setElementId(QStringLiteral("SVGF2M1"));
          f2->setSharedRenderer(renderer);
          f2->setElementId(QStringLiteral("SVGF2M2"));
          f3->setSharedRenderer(renderer);
          f3->setElementId(QStringLiteral("SVGF2M3"));
          f4->setSharedRenderer(renderer);
          f4->setElementId(QStringLiteral("SVGF3M1"));
          f5->setSharedRenderer(renderer);
          f5->setElementId(QStringLiteral("SVGF4M1"));
          f6->setSharedRenderer(renderer);
          f6->setElementId(QStringLiteral("SVGF4M2"));
      
          ui->function1->setScene(scene);
          scene->addItem(f1);
          ui->function1->show();
      
          ui->function2->setScene(scene);
          scene->addItem(f2);
          ui->function2->show();
      
          ui->function3->setScene(scene);
          scene->addItem(f3);
          ui->function3->show();
      
          ui->function4->setScene(scene);
          scene->addItem(f4);
          ui->function4->show();
      
          ui->function5->setScene(scene);
          scene->addItem(f5);
          ui->function5->show();
      
          ui->function6->setScene(scene);
          scene->addItem(f6);
          ui->function6->show();
      
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      

      Is this corret, or did I miss some important steps?

      If I build the Project, it tells me it crashed but without any error.

      11:04:29: Starting /home/g/workspace/build-unimogQT_secPanel-Desktop_Qt_5_9_9_GCC_64bit-Debug/unimogQT_secPanel ...
      11:04:29: The program has unexpectedly finished.
      11:04:29: The process was ended forcefully.
      11:04:29: /home/g/workspace/build-unimogQT_secPanel-Desktop_Qt_5_9_9_GCC_64bit-Debug/unimogQT_secPanel crashed.

      Thx in Advance

      gewitt

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @gewitt last edited by

        @gewitt said in Display different QGraphicsSvgItems in QGraphicViews:

        f1->setSharedRenderer(renderer);
        f1->setElementId(QStringLiteral("SVGF2M1"));
        f2->setSharedRenderer(renderer);
        f2->setElementId(QStringLiteral("SVGF2M2"));
        f3->setSharedRenderer(renderer);
        f3->setElementId(QStringLiteral("SVGF2M3"));
        f4->setSharedRenderer(renderer);
        f4->setElementId(QStringLiteral("SVGF3M1"));
        f5->setSharedRenderer(renderer);
        f5->setElementId(QStringLiteral("SVGF4M1"));
        f6->setSharedRenderer(renderer);
        f6->setElementId(QStringLiteral("SVGF4M2"));

        All these f* are dangling pointers! You did not allocate memory and assign the address (f1 = new ...)!

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        G 1 Reply Last reply Reply Quote 3
        • G
          gewitt @jsulm last edited by

          @jsulm
          thx for the fast reply!

          well now it compiles, but it doesnt show my svgs in the GraphicsViews

          7c8188eb-a262-4505-a8c6-8a03470e9b4f-image.png

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @gewitt last edited by mrjj

            @gewitt

            Hi
            Well it cant find your svg , i would guess
            "Icons.svg"
            It will look in the build folder for it as there is where exe is run from.

            It wont find it in the project folder unless you use an absolute path.

            You can also use a res file
            https://doc.qt.io/qt-5/resources.html

            1 Reply Last reply Reply Quote 3
            • G
              gewitt last edited by

              I made a Resource file:

              <RCC>
                  <qresource prefix="/assets">
                      <file>Icons.svg</file>
                  </qresource>
              </RCC>
              

              and call it like

              QSvgRenderer(QStringLiteral(":/assets/Icons.svg"));
              

              nothing changed :(

              mrjj 1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion @gewitt last edited by

                @gewitt
                And you did a full recompile so the res file was made ?
                Test with a QLabel that it can show the svg.
                It might be related to setElementId or somethign else so start testing it can show the
                SVG normally with qlable as a window. very fast test.

                1 Reply Last reply Reply Quote 0
                • G
                  gewitt last edited by

                  i did use the wrong ids like it seems,
                  but now i get all the Svgs overlapped in every GraphicsView.
                  how can i fix this?
                  6a7b8a3b-9f87-4b45-a56f-35cce8e6d158-image.png

                  mrjj 1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion @gewitt last edited by

                    @gewitt

                    Well it seems that it render it all.´even when using
                    setElementId
                    I wonder if its due to the render being shared.
                    Make some small test to
                    verify it can indeed render just the element and not whole SVG.

                    1 Reply Last reply Reply Quote 0
                    • G
                      gewitt last edited by

                      seems like i need a new scene for every Graphicsview then it is working :)

                          scene1 = new QGraphicsScene(this);
                          scene2 = new QGraphicsScene(this);
                          scene3 = new QGraphicsScene(this);
                          scene4 = new QGraphicsScene(this);
                          scene5 = new QGraphicsScene(this);
                          scene6 = new QGraphicsScene(this);
                      
                          QSvgRenderer *renderer = new QSvgRenderer(QStringLiteral(":/assets/Icons.svg"));
                      
                      
                          f1 = new QGraphicsSvgItem();
                          f2 = new QGraphicsSvgItem();
                          f3 = new QGraphicsSvgItem();
                          f4 = new QGraphicsSvgItem();
                          f5 = new QGraphicsSvgItem();
                          f6 = new QGraphicsSvgItem();
                      
                      
                          f1->setSharedRenderer(renderer);
                          f1->setElementId(QStringLiteral("ring"));
                          ui->function1->setScene(scene1);
                          scene1->addItem(f1);
                          ui->function1->show();
                      
                      
                          f2->setSharedRenderer(renderer);
                          f2->setElementId(QStringLiteral("EselKontur"));
                          ui->function2->setScene(scene2);
                          scene2->addItem(f2);
                          ui->function2->show();
                      
                          f3->setSharedRenderer(renderer);
                          f3->setElementId(QStringLiteral("Kopf"));
                          ui->function3->setScene(scene3);
                          scene3->addItem(f3);
                          ui->function3->show();
                      
                          f4->setSharedRenderer(renderer);
                          f4->setElementId(QStringLiteral("Kreis2"));
                          ui->function4->setScene(scene4);
                          scene4->addItem(f4);
                          ui->function4->show();
                      
                          f5->setSharedRenderer(renderer);
                          f5->setElementId(QStringLiteral("rectTop"));
                          ui->function5->setScene(scene2);
                          scene5->addItem(f5);
                          ui->function5->show();
                      
                          f6->setSharedRenderer(renderer);
                          f6->setElementId(QStringLiteral("rectVerstellpumpeHorizontal"));
                          ui->function6->setScene(scene6);
                          scene6->addItem(f6);
                          ui->function6->show();
                      

                      Thx for your help :)

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post