How to create any number of QGraphicsView Dynamically
-
-
show the code please.
-
show the code please.
@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.
-
I did not understand your response. Do you have issue still or not ?
-
I did not understand your response. Do you have issue still or not ?
@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:
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?
-
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 ?
-
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 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. -
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); }
-
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.
-
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.
@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?
-
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 :)