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
{
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
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:
qt output.jpg
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.
Bookmarks