Results 1 to 6 of 6

Thread: How to show items in the same position saved in QGraphicsView/Scene ?

  1. #1
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    9
    Qt products
    Qt4

    Default How to show items in the same position saved in QGraphicsView/Scene ?

    Hi to all,
    I'm having trouble showing the image saved in a QGraphicsScene. I need to save multiple image items in QGraphicsView and reload them to do something. My solution is to save each item as an independent image file and write down each scene position into a record for loading. In my process, if the GraphicsView has the same size in saving/loading, image files can be loaded and shown in the same position as they saved. But if I use a larger/smaller window to load images, the images are not located in the same position (corresponding to the top-left corner of the view).

    My saving process
    Qt Code:
    1. QTextStream ts(&file);
    2. ..
    3. QGraphicsPixmapItem* it = qgraphicsitem_cast<QGraphicsPixmapItem*>(scene()->items().at(i));
    4. tag = QString("%1/%2.png").arg(path).arg(i); // filename
    5. It->pixmap (). save (tag); // save image item
    6. ts << "\t\t" << it->x() << " " << it->y() << " " << tag << "\n"; // save the scene position and the file name
    To copy to clipboard, switch view to plain text mode 


    My loading process
    Qt Code:
    1. QTextStream ts(&file);
    2. ..
    3. ts >> x >> y >> tag;
    4. QImage img(tag); // read the image by filename
    5. QGraphicsPixmapItem* item = new QGraphicsPixmapItem(pixmap, 0, scene());
    6. item->setPos(x, y);
    To copy to clipboard, switch view to plain text mode 

    It's sorry that I have no idea about it. Thank you.

    The image file saved in a small window.
    pos_smallwnd.jpg

    After loading, the image seems moved
    pos_largewnd.jpg

    What I want .. located at fixed position to the top-left corner of GraphicsView
    pos_I_want.jpg
    Last edited by chiaminhsu; 12th November 2013 at 10:38.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show items in the same position saved in QGraphicsView/Scene ?

    One solution is to save relative position coordinates rather than absolute values.
    What I mean is, for example, lets say we have an image of size = (100,100) placed on pos = (50,50) on a scene with scene size = (200,200).
    If you save only absolute values of image size and position, you'll have a problem if the scene size changes after loading.
    Instead, you can save image parameters like size = (0.5,0.5) and pos = (0.25,0.25) (or 50% and 25%, it means - how much of the whole scene size is occupied by this image, 0.5 = 100/200, 0.25 = 50/200...). Now if you load it on scene with scene size = (400,400), you simply rescale the values :
    new_size = (0.5*400, 0.5*400)=(200,200), new_pos = (0.25*400,0.25*400)=(100,100). Simple, isn't it ?
    Of course if scenes aspect ratio is changed, your image will change as well, but it's up to you how you want to deal with it.
    Another solution is to save scene size as well and rescale everything after loading.
    I won't give you working code as I think it's better if you work it out yourself.

  3. #3
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    9
    Qt products
    Qt4

    Default Re: How to show items in the same position saved in QGraphicsView/Scene ?

    Sorry Stampede, I did not explain my request clearly and you may get wrong with me. The last figure explain what I want. I dont want to enlarge the image. I hope that they can keep the same size and the position offset to the left-top corder of me

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show items in the same position saved in QGraphicsView/Scene ?

    keep the same size and the position offset to the left-top corder
    Define what "same" means in term "same size and offset".
    Suppose we have a scene, size = (100,100) and image placed at (10,10). Now you load it on new scene, which has size=(200,200). "Same" means to you
    1) offset should still be (10,10)
    or
    2) offset should be (20,20) = 10% of scenes dimension as previously
    or
    3) <some other definition>

    which one is it ?
    case 1) - save absolute values of x,y and set it via setPos(x,y) after load
    case 2) - use algorithm I described above only for position component
    case 3) - (?)

  5. #5
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    9
    Qt products
    Qt4

    Default Re: How to show items in the same position saved in QGraphicsView/Scene ?

    My problem is ...

    I have some early sketch data recorded in view coordinate but the image objects are saved in scene coordinate. So when I open the file in different window size, these images seems not located on the same position vs. the left-top corner of the window.

    The leader said that I can fix the view resolution, so I can save/load images both in view coordinate and additem() in scene coordinate. It works...

    Sorry Stampede deeply that you spend time for my question but I did not understand well my request and my problem clearly. Sorry sorry sorry ...

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to show items in the same position saved in QGraphicsView/Scene ?

    Ok, no problem, good that it works now

Similar Threads

  1. QGraphicsview - how to organize scene and items
    By metRo_ in forum Qt Programming
    Replies: 0
    Last Post: 10th April 2012, 01:39
  2. QGraphicsView - show mouse cursor position in status bar
    By mwgobetti in forum Qt Programming
    Replies: 5
    Last Post: 19th November 2011, 19:34
  3. Cursor Position in scene
    By rogerholmes in forum Newbie
    Replies: 2
    Last Post: 12th March 2010, 16:31
  4. Position of Items in QGraphicsScene/QGraphicsView
    By StefanK2 in forum Qt Programming
    Replies: 11
    Last Post: 7th July 2009, 15:04
  5. Cursor scene position
    By xgoan in forum Qt Programming
    Replies: 1
    Last Post: 26th December 2006, 14:51

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.