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 create any number of QGraphicsView Dynamically
Forum Updated to NodeBB v4.3 + New Features

How to create any number of QGraphicsView Dynamically

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.4k 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.
  • R Offline
    R Offline
    R_Irudezu
    wrote on 9 Nov 2018, 03:40 last edited by
    #1

    I want to create QGraphicsView with QGraphicsScene when button clicked. It's gonna be shown for 5 second and then be deleted. Everytime i pressed the button, the new QGraphicViews need to be created at random locations of UI. How can i achieve this?

    Keizoku wa chikaranari.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 9 Nov 2018, 04:49 last edited by
      #2

      show the code please.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      R 1 Reply Last reply 9 Nov 2018, 05:12
      0
      • D dheerendra
        9 Nov 2018, 04:49

        show the code please.

        R Offline
        R Offline
        R_Irudezu
        wrote on 9 Nov 2018, 05:12 last edited by R_Irudezu 11 Sept 2018, 05:48
        #3

        @dheerendra
        widget.h:

        #ifndef WIDGET_H
        #define WIDGET_H
        
        #include <QWidget>
        #include <QGraphicsItem>
        #include <QGraphicsView>
        
        namespace Ui {
        class Widget;
        }
        
        class Widget : public QWidget
        {
            Q_OBJECT
        
        
        public:
            explicit Widget(QWidget *parent = nullptr);
            ~Widget();
        
            QGraphicsScene *scene;
            QTimeLine *timeline;
            QGraphicsView *gw;
            QGraphicsView *newGw;
        
            void drawNewGraphicsView
            (
                QGraphicsView *gw_func,
                uint16_t fixedHeight,
                uint16_t fixedWidth,
                uint16_t moveX,
                uint16_t moveY,
                uint16_t lifeTime
            );
        
        private slots:
            void on_pushButton_clicked();
        
        private:
            Ui::Widget *ui;
        };
        
        #endif // WIDGET_H
        
        

        widget.cpp:

        #include "widget.h"
        #include "ui_widget.h"
        #include <QTimeLine>
        
        Widget::Widget(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::Widget)
        {
            ui->setupUi(this);
        
            timeline = new QTimeLine;
            gw = new QGraphicsView(ui->widget);
            drawNewGraphicsView(gw, 80, 80, 150, 150, 3);
        }
        
        Widget::~Widget()
        {
            delete ui;
        }
        
        void Widget::drawNewGraphicsView
        (
            QGraphicsView *gw_func,
            uint16_t fixedHeight,
            uint16_t fixedWidth,
            uint16_t moveX,
            uint16_t moveY,
            uint16_t lifeTime
        )
        {
            timeline->setDuration (1000);
            timeline->setCurveShape (QTimeLine::CosineCurve);
            QObject::connect (timeline, SIGNAL(finished()), timeline, SLOT(deleteLater()));
        
            scene = new QGraphicsScene(this);
        
            newGw = gw_func;
            newGw->setScene(scene);
            newGw->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            newGw->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );
            newGw->setStyleSheet("background-color: transparent; border: none;");
            newGw->setFixedHeight(fixedHeight);
            newGw->setFixedWidth(fixedWidth);
            newGw->move(moveX,moveY);
            newGw->setDisabled(true); //scrolling inactivate
            newGw->raise();
            newGw->show();
        
            scene->setSceneRect (-75, -75, 150, 150);
            setupRot (timeline, scene->addEllipse (rect(35), QPen (QBrush (QColor (0, 153, 255)), 2.4, Qt::DotLine)));
            timeline->setLoopCount (0); 
            timeline->start();
        }
        
        void Widget::on_pushButton_clicked()
        {
            // adding new graphicview objects to randomly locations
            // lifetime 5 second and want to delete it
        }
        
        

        I couldn't do what i want. I'm gonna create them many, at least mean 3 graphicsView in one second, so i do now want to do memory leak.

        Keizoku wa chikaranari.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 9 Nov 2018, 05:35 last edited by
          #4

          I did not understand your response. Do you have issue still or not ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          R 1 Reply Last reply 9 Nov 2018, 05:44
          0
          • D dheerendra
            9 Nov 2018, 05:35

            I did not understand your response. Do you have issue still or not ?

            R Offline
            R Offline
            R_Irudezu
            wrote on 9 Nov 2018, 05:44 last edited by R_Irudezu 11 Sept 2018, 05:46
            #5

            @dheerendra Sir the issue is that graphicsView objects defined in header are in specific number. I do not want to declare them before compile, i want to create graphicsView objects at runtime:

            0_1541742106185_2gif.gif

            you can see in the media, i created 3 graphicsView objects, second and third one is creating like that:

            void Widget::on_pushButton_clicked()
            {
                uint16_t x = (qrand() % 50)*3 ; qDebug() << x;
                uint16_t y = (qrand() % 35)*3 ; qDebug() << x;
            
                gw1 = new QGraphicsView(ui->widget);
                drawNewGraphicsView(gw1, 120, 120, x, x*2, 5);
            
                gw2 = new QGraphicsView(ui->widget);
                drawNewGraphicsView(gw2, 130, 160, y, y*2+5, 8);
            }
            
            void Widget::on_pushButton_2_clicked()
            {
                delete gw1;
                delete gw2;
            }
            

            Like i said and like you see, these number of objects are specific, i do not want to specify number of its, what i want is creating how much i want to create of this objects at a (x)second, show them and delete them. Can you help me about that point?

            Keizoku wa chikaranari.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 9 Nov 2018, 05:57 last edited by
              #6

              How do you get the input on how many number of objects to create ? Is it based on some input ? How do you decide the number of GraphicsView object ?

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              R 1 Reply Last reply 9 Nov 2018, 06:05
              0
              • D dheerendra
                9 Nov 2018, 05:57

                How do you get the input on how many number of objects to create ? Is it based on some input ? How do you decide the number of GraphicsView object ?

                R Offline
                R Offline
                R_Irudezu
                wrote on 9 Nov 2018, 06:05 last edited by R_Irudezu 11 Sept 2018, 06:06
                #7

                @dheerendra it is exactly based on some outputs that an external library callback function gives me. I mean i got 3 * (x,y,w,h)
                locations at one second and i have to show this graphic object (the blue cycle in the GIF ) at these locations.

                Keizoku wa chikaranari.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on 9 Nov 2018, 07:20 last edited by dheerendra 11 Sept 2018, 07:20
                  #8

                  when the callback happens store that value in some member variable. Use that member variable to decide on how many Views you need to create.

                  QGraphicsView **views; // Declare this as member variable in your class.
                  
                  views = new QGraphicsView* [count]
                  for(int i=0; i<count;i++ {
                      views[i] = new QGraphicsView(ui->widget);
                      drawNewGraphicsView(views[i], 130, 160, y, y*2+5, 8);
                  }
                  

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Asperamanca
                    wrote on 9 Nov 2018, 10:03 last edited by Asperamanca 11 Sept 2018, 10:03
                    #9

                    You could simply connect the finished() signal of your timeline to the deleteLater() slots of the QGraphicsScene and QGraphicsView. That way, they are automatically deleted when the timeline expires.

                    EDIT: No need to keep a pointer to those scenes, unless you need to access them at a later time in your code.

                    R 1 Reply Last reply 9 Nov 2018, 13:46
                    0
                    • A Asperamanca
                      9 Nov 2018, 10:03

                      You could simply connect the finished() signal of your timeline to the deleteLater() slots of the QGraphicsScene and QGraphicsView. That way, they are automatically deleted when the timeline expires.

                      EDIT: No need to keep a pointer to those scenes, unless you need to access them at a later time in your code.

                      R Offline
                      R Offline
                      R_Irudezu
                      wrote on 9 Nov 2018, 13:46 last edited by R_Irudezu 11 Sept 2018, 15:23
                      #10

                      @Asperamanca It's seems logical about deleting objects after shown but is it possible to define a connect that valid for every QGraphicView object created?

                      Keizoku wa chikaranari.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        R_Irudezu
                        wrote on 10 Nov 2018, 12:04 last edited by
                        #11

                        So i wrote a sample based on @dheerendra and @Asperamanca 's replies.

                            //in the header file defined 
                            QGraphicsView **views;
                            QTimeLine **timelineSet;
                        
                            // connect both view and scene objects' deleteLater slots to 
                            // timeline's finished signal
                            QGraphicsView::connect(timeline, SIGNAL(finished()), newGw, SLOT(deleteLater()));
                            QGraphicsScene::connect(timeline, SIGNAL(finished()), scene, SLOT(deleteLater()));
                        
                            // in the callback
                            views = new QGraphicsView* [5]; //specific 
                            for(int i=0; i<5;i++)
                            {
                                y += 25 + i*12;
                                views[i] = new QGraphicsView(ui->widget);
                                timelineSet[i] = new QTimeLine();
                                drawNewGraphicsView(views[i], 130, 160, y*2+5,  y*2+25, timelineSet[i], lifeT*i%3, 5);
                            }
                        

                        So it's okay now, just like i want. I'm creating objects, showing them for a couple of seconds and delete them automatically with deleteLater().

                        Thanks for any help :)

                        Keizoku wa chikaranari.

                        1 Reply Last reply
                        0

                        1/11

                        9 Nov 2018, 03:40

                        • Login

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