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. How to add more than one EllipseItem to a Scene..?
Forum Updated to NodeBB v4.3 + New Features

How to add more than one EllipseItem to a Scene..?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 3.3k 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.
  • R Offline
    R Offline
    Rohith
    wrote on last edited by
    #1

    I want to add around 11-12 QGraphicsEllipseItems to a scene. How can i achieve this using

    ellipseOne = scene->addEllipse(0,0,10,10,blackpen,redBrush);
    .
    .
    .
    .
    ellipseTwelve  = scene->addEllipse(0,0,10,10,blackpen,redBrush);
    

    Apart from this is there any other way.

    Thanks in advance,
    Rohith.G

    C Chris KawaC joeQJ 3 Replies Last reply
    0
    • R Rohith

      I want to add around 11-12 QGraphicsEllipseItems to a scene. How can i achieve this using

      ellipseOne = scene->addEllipse(0,0,10,10,blackpen,redBrush);
      .
      .
      .
      .
      ellipseTwelve  = scene->addEllipse(0,0,10,10,blackpen,redBrush);
      

      Apart from this is there any other way.

      Thanks in advance,
      Rohith.G

      C Offline
      C Offline
      c64zottel
      wrote on last edited by
      #2

      @Rohith The documentation sais:

      To add items to a scene, you start off by constructing a QGraphicsScene object. Then, you have two options: either add your existing QGraphicsItem objects by calling addItem(), or you can call one of the convenience functions addEllipse(), addLine(), addPath(), addPixmap(), addPolygon(), addRect(), or addText(), which all return a pointer to the newly added item. The dimensions of the items added with these functions are relative to the item's coordinate system, and the items position is initialized to (0, 0) in the scene.

      From here: http://doc.qt.io/qt-5/qgraphicsscene.html#details

      1 Reply Last reply
      3
      • R Rohith

        I want to add around 11-12 QGraphicsEllipseItems to a scene. How can i achieve this using

        ellipseOne = scene->addEllipse(0,0,10,10,blackpen,redBrush);
        .
        .
        .
        .
        ellipseTwelve  = scene->addEllipse(0,0,10,10,blackpen,redBrush);
        

        Apart from this is there any other way.

        Thanks in advance,
        Rohith.G

        Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Rohith said in How to add more than one EllipseItem to a Scene..?:

        Apart from this is there any other way.

        Yes, use a for loop and store the items in a container, not named individual items.

        1 Reply Last reply
        2
        • R Rohith

          I want to add around 11-12 QGraphicsEllipseItems to a scene. How can i achieve this using

          ellipseOne = scene->addEllipse(0,0,10,10,blackpen,redBrush);
          .
          .
          .
          .
          ellipseTwelve  = scene->addEllipse(0,0,10,10,blackpen,redBrush);
          

          Apart from this is there any other way.

          Thanks in advance,
          Rohith.G

          joeQJ Offline
          joeQJ Offline
          joeQ
          wrote on last edited by joeQ
          #4

          @Rohith Hi friend, I think you are new for programing. so, if you want to become well in this. please write more and more code. This question is not complexity.

          Snippet Code

          #include <QGridLayout>
          #include <QGraphicsEllipseItem>
          #include <QGraphicsView>
          #include <QGraphicsScene>
          
          Widget::Widget(QWidget *parent) :
              QWidget(parent),
              ui(new Ui::Widget)
          {
              ui->setupUi(this);
          
              ///< QGraphicsScene *scene = new QGraphicsScene;
              ///< QGraphicsView  *view  = new QGraphicsView(this);
              
              QGraphicsView  *view  = new QGraphicsView;
              QGraphicsScene *scene = new QGraphicsScene(this); ///< Note: this pointer, if not have, will let memory leak
              view->setScene(scene);
          
              int x,y,w,h;
              int num = 10;
              for(int i=0; i < num; i++){
                  x = y = i * 10;
                  w = h = i * 10;
                  scene->addItem(new QGraphicsEllipseItem(QRect(x,y,w,h)));
              }
          
              QGridLayout* lyt = new QGridLayout;
              lyt->addWidget(view);
              setLayout(lyt);
          }
          

          Just do it!

          1 Reply Last reply
          1
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            @joeQ That's wrong. ui->setupUi(this); sets up the ui and most probably sets a layout already, so instead of

            QGridLayout* lyt = new QGridLayout;
            lyt->addWidget(view);
            setLayout(lyt);
            

            EDIT: I haven't noticed that it's a QWidget not a QMainWindow, so you'll need a layout indeed, but setupUi probably sets it anyway.

            Also the view does not take ownership of the scene so you've got a memory leak. Instead of giving a parent to the view you should give it to the scene:

            QGraphicsScene *scene = new QGraphicsScene(this);
            QGraphicsView  *view  = new QGraphicsView();
            
            C joeQJ 3 Replies Last reply
            2
            • Chris KawaC Chris Kawa

              @joeQ That's wrong. ui->setupUi(this); sets up the ui and most probably sets a layout already, so instead of

              QGridLayout* lyt = new QGridLayout;
              lyt->addWidget(view);
              setLayout(lyt);
              

              EDIT: I haven't noticed that it's a QWidget not a QMainWindow, so you'll need a layout indeed, but setupUi probably sets it anyway.

              Also the view does not take ownership of the scene so you've got a memory leak. Instead of giving a parent to the view you should give it to the scene:

              QGraphicsScene *scene = new QGraphicsScene(this);
              QGraphicsView  *view  = new QGraphicsView();
              
              C Offline
              C Offline
              c64zottel
              wrote on last edited by
              #6

              @Chris-Kawa In that case, now there is another memory leak; for the view. So, it should probably be:

              QGraphicsScene *scene = new QGraphicsScene(this);
              QGraphicsView  *view  = new QGraphicsView(this);
              
              1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                @joeQ That's wrong. ui->setupUi(this); sets up the ui and most probably sets a layout already, so instead of

                QGridLayout* lyt = new QGridLayout;
                lyt->addWidget(view);
                setLayout(lyt);
                

                EDIT: I haven't noticed that it's a QWidget not a QMainWindow, so you'll need a layout indeed, but setupUi probably sets it anyway.

                Also the view does not take ownership of the scene so you've got a memory leak. Instead of giving a parent to the view you should give it to the scene:

                QGraphicsScene *scene = new QGraphicsScene(this);
                QGraphicsView  *view  = new QGraphicsView();
                
                joeQJ Offline
                joeQJ Offline
                joeQ
                wrote on last edited by
                #7

                @Chris-Kawa (⊙o⊙)!, Thank U very much. I get it. Thank u.

                Just do it!

                1 Reply Last reply
                1
                • Chris KawaC Online
                  Chris KawaC Online
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  @c64zottel No, there's not. When a widget is put in a layout it gets re-parented to the widget governed by the layout. Similarly when it is set as central widget the main window becomes its parent. Widgets are released by their parents so there's no leak.

                  C 1 Reply Last reply
                  3
                  • Chris KawaC Chris Kawa

                    @c64zottel No, there's not. When a widget is put in a layout it gets re-parented to the widget governed by the layout. Similarly when it is set as central widget the main window becomes its parent. Widgets are released by their parents so there's no leak.

                    C Offline
                    C Offline
                    c64zottel
                    wrote on last edited by
                    #9

                    @Chris-Kawa Argh..., I missed that.

                    1 Reply Last reply
                    0
                    • Chris KawaC Chris Kawa

                      @joeQ That's wrong. ui->setupUi(this); sets up the ui and most probably sets a layout already, so instead of

                      QGridLayout* lyt = new QGridLayout;
                      lyt->addWidget(view);
                      setLayout(lyt);
                      

                      EDIT: I haven't noticed that it's a QWidget not a QMainWindow, so you'll need a layout indeed, but setupUi probably sets it anyway.

                      Also the view does not take ownership of the scene so you've got a memory leak. Instead of giving a parent to the view you should give it to the scene:

                      QGraphicsScene *scene = new QGraphicsScene(this);
                      QGraphicsView  *view  = new QGraphicsView();
                      
                      joeQJ Offline
                      joeQJ Offline
                      joeQ
                      wrote on last edited by
                      #10

                      @Chris-Kawa Hi, I used the Debug -> Memcheck to check memory leak of my code. There wasn't any memory leak. Is this valgrind tool inaccurate ?

                      Just do it!

                      1 Reply Last reply
                      1
                      • Chris KawaC Online
                        Chris KawaC Online
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @joeQ I don't know. I never used valgrind. But just add

                        connect(scene , &QGraphicsScene::destroyed, []{ qDebug() << "destroyed!"; });
                        

                        and see that the destructor is never called if there's no parent.

                        joeQJ 1 Reply Last reply
                        3
                        • Chris KawaC Chris Kawa

                          @joeQ I don't know. I never used valgrind. But just add

                          connect(scene , &QGraphicsScene::destroyed, []{ qDebug() << "destroyed!"; });
                          

                          and see that the destructor is never called if there's no parent.

                          joeQJ Offline
                          joeQJ Offline
                          joeQ
                          wrote on last edited by
                          #12

                          @Chris-Kawa Yes, You are right. thank u again.

                          Just do it!

                          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