PDA

View Full Version : Getting coordinates of all QGraphicsItem s on a QGraphicsScene



xd123
19th April 2020, 17:50
Hi, I'm developing a GUI application in Qt where a user can annotate a selected image with various shapes of QGraphicsItem, for example QGraphicsRectItem, QGraphicsPolygonItem, and so on. Each of my shapes has a distinct button for adding the shape like so:



void MainWindow::on_pushButton_11_clicked() // rectangle shape creator
{
QGraphicsRectItem* rectItem = new QGraphicsRectItem(QRectF(0, 0, 320, 240));
QColor brush_color(Qt::blue); //fill color
brush_color.setAlpha(50); // alpha index makes brush color more opaque
QPen blackpen(Qt::black); //border color
blackpen.setWidth(2); // border width
rectItem->setBrush(brush_color); // using brush color
rectItem->setPen(blackpen);
rectItem->setFlag(QGraphicsItem::ItemIsMovable); // making the object dragable across graphics view
scene->addItem(rectItem);
}


Whenever the user presses a shape button the shape is created in the center of the scene and can be then moved. What I am looking to do now is to retrieve all graphics items on the scene and their respective coordinates to a text file so that when the user is done, (s)he can save the work and load the annotation shapes at the same coordinates next time.

So far I have done the most progress with QList<QGraphicsItem*> graphicsItemList = scene->items() which enables me to get all the items, but strangely enough I don't know if the output I'm getting is correct as for example with 3 shapes (Rectangle, Ellipse and a Triangle) I get this output to my console from graphicsItemList:
13406

In case that this is the correct and expected output my question would be, how would I write down all this information into a text file so that Qt could re-create these shapes at the given coordinates?

My format for the text file would be that each line represents a shape(with for example a number, 1 being rectangle, 2 ellipse and so on) followed by its coordinates like so:
1 0.3095703125 0.6815519765739385 0.359375 0.4260614934114202

At least that is the format that makes the most sense in my head, any suggestions, questions and potential fixes are welcome, thanks.

ChristianEhrlicher
19th April 2020, 19:08
Ypu can get all items on a scene with QGraphicsScene::items() (https://doc.qt.io/qt-5/qgraphicsscene.html#items)

xd123
19th April 2020, 20:33
great. Is there a way you know of that i could store these coordinates in a text file on a separate line each, so my question would be how do I recognise where is the end of coordinates of 1 shape and begining of another?

ChristianEhrlicher
19th April 2020, 21:16
This is up to you - maybe separate them with a newline.

xd123
20th April 2020, 00:32
yeah I know how to get the data into a file. What i don't know is since you are saying all of that data are valid coordinates to my 3 shapes drawn on my scene, then how would you distinguish where does for example the first shape and its respective coordinates end, and where does the set of coordinates for second shape begin? Because there is no way to tell from the Qlist I'm getting

d_stranz
20th April 2020, 04:41
Your screenshot is hard to read, but it looks like it is a qDebug() dump of the QList of QGraphicsItem instances. This is not how you will write your list to a file. (Yes, I know you can direct QDebug output to a file, but it doesn't provide the information you want in a very useful format).

You will write a loop, get each QGraphicsItem pointer from the list, determine its type(), coordinates, size, and whatever else your want. You then take all of that, format it into a QString, and write that QString to the file in a single line. The next item goes on the next line, and so forth.