Results 1 to 6 of 6

Thread: Getting coordinates of all QGraphicsItem s on a QGraphicsScene

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Getting coordinates of all QGraphicsItem s on a QGraphicsScene

    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:

    Qt Code:
    1. void MainWindow::on_pushButton_11_clicked() // rectangle shape creator
    2. {
    3. QGraphicsRectItem* rectItem = new QGraphicsRectItem(QRectF(0, 0, 320, 240));
    4. QColor brush_color(Qt::blue); //fill color
    5. brush_color.setAlpha(50); // alpha index makes brush color more opaque
    6. QPen blackpen(Qt::black); //border color
    7. blackpen.setWidth(2); // border width
    8. rectItem->setBrush(brush_color); // using brush color
    9. rectItem->setPen(blackpen);
    10. rectItem->setFlag(QGraphicsItem::ItemIsMovable); // making the object dragable across graphics view
    11. scene->addItem(rectItem);
    12. }
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting coordinates of all QGraphicsItem s on a QGraphicsScene

    Ypu can get all items on a scene with QGraphicsScene::items()

  3. #3
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Getting coordinates of all QGraphicsItem s on a QGraphicsScene

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting coordinates of all QGraphicsItem s on a QGraphicsScene

    This is up to you - maybe separate them with a newline.

  5. #5
    Join Date
    Apr 2020
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Getting coordinates of all QGraphicsItem s on a QGraphicsScene

    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

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Getting coordinates of all QGraphicsItem s on a QGraphicsScene

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 10
    Last Post: 15th November 2012, 15:13
  2. QGraphicsItem coordinates, again
    By d_stranz in forum Qt Programming
    Replies: 15
    Last Post: 20th June 2011, 22:10
  3. Replies: 7
    Last Post: 21st March 2010, 04:11
  4. Replies: 0
    Last Post: 24th November 2008, 09:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.