How to add more than one EllipseItem to a Scene..?
-
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 -
@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
-
@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. -
@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); }
-
@joeQ That's wrong.
ui->setupUi(this);
sets up the ui and most probably sets a layout already, so instead ofQGridLayout* 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();
-
@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);
-
@Chris-Kawa (⊙o⊙)!, Thank U very much. I get it. Thank u.
-
@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.
-
@Chris-Kawa Argh..., I missed that.
-
@Chris-Kawa Hi, I used the
Debug -> Memcheck
to check memory leak of my code. There wasn't any memory leak. Is thisvalgrind
tool inaccurate ? -
@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.
-
@Chris-Kawa Yes, You are right. thank u again.