Results 1 to 9 of 9

Thread: Position in a QPixMap created from a QGraphicsScene

  1. #1
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Question Position in a QPixMap created from a QGraphicsScene

    I am creating a QPixMap from a QGraphicsScene. I then use the QPixMap in a QGraphicsPixMapItem in another QGraphicsScene. Here's the code used to create the QPixMap:

    Qt Code:
    1. //
    2. // JSL - CreateQPixmap
    3. //
    4. QPixmap EDrawListView::createQPixmap()
    5. {
    6. unselectAll();
    7.  
    8. QRectF sourceRect = scene()->itemsBoundingRect();
    9. sourceRect.setX(sourceRect.x()-1);
    10. sourceRect.setY(sourceRect.y()-1);
    11. QRectF targetRect(0, 0, sourceRect.width()+2, sourceRect.height()+2);
    12. QPixmap thePixmap(sourceRect.width()+2, sourceRect.height()+2);
    13. thePixmap.fill(Qt::transparent);
    14. QPainter p(&thePixmap);
    15. scene()->render(&p, targetRect, sourceRect);
    16. p.end();
    17. return(thePixmap);
    18. }
    To copy to clipboard, switch view to plain text mode 

    The problem is that once the scene has been turned into a QPixMap, I still need to know where a certain item on that scene was in coordinates that relate to the QPixMap, not to the original Scene before the QPixmap was created. If I look at the pos value of the item as it was on the scene, it doesn't relate to the position of the image of the item on the QPixMap. (Because of the itemsBoundingRect code in the call above, I imagine.)
    Now that I am writing this, I imagine that saving the itemsboundingRect might be the clue. If I save this rect, then I can use it to offset the pos of the item to find it's location on the QPixMap.

    If anyone has any thoughts or suggestions, I would appreciate hearing them.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Position in a QPixMap created from a QGraphicsScene

    What are you doing with the pixmap afterwards and what is the final effect you want to obtain?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Position in a QPixMap created from a QGraphicsScene

    Wysota,

    Thanks for your reply. The QPixMap is embedded in a QGraphicsPixMapItem, and inserted in a QGraphicsScene. The final effect I have to have is to make it so when I hover over part of this QGraphicsPixMapItem, I can tell over which of the QGraphicsItems in the QGraphicsScene used to make the QGraphicsPixMap I am hovering.

    (Basically it's a QGraphicsScene within a QGraphicsScene, but I am taking a QPixMap of the smaller scene.)

    The final effect is you have a QPixMap on the scene, you double click it, it opens a window containing the contents of the QPixMap, you edit it, close it, and it recreates the QPixMap based on the scene in the opened window.

    As I said, the problem now is hovering over part of the QPixMap, and identifying which part of the 'subscene' I am over. The scene does not still exist as a scene, as it is constructed and destroyed when it's window is opened and closed, and the pos coordinates are wrong, as described in my first post.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Position in a QPixMap created from a QGraphicsScene

    I dont think using 2 scenes would be good for your requirements.
    What I understand is, you need to show a small map of the whole area, like in games etc.

    What you need is 2 views of the same scene. If you look at the 40000 chips demo in Qt Demo, you will find there are 4 views of the same scene. Zoom out completely in one view. Now from other views, if you move items, it will be reflected in the zoomed out view.
    Similarly you can keep one view as the main, make the second view small size and fit the whole scene in it. and you will get kinda small map ..

    Hope I understood your requirement correctly

  5. #5
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Position in a QPixMap created from a QGraphicsScene

    @aamer4yu

    Thanks for the reply. I don't think that you did understand my requirement correctly. I already have the two scene stuff working quite well, and it is exactly what I need. The intent of the smaller scene is just to be a way to edit the QPixMap on the QGraphicsPixMapItem on the larger scene. There is no relationship between what is shown on the QPixMap on the QGraphicsPixMapItem and the larger scene. It is not a view of the larger scene. I have all this working quite well, you double click on the QPixMapGraphicsItem, and it opens a window where you edit the contents of the picture in a QGraphicsScene, then you close it, and it recreates the QPixMap. The problem is the coordinate system, and finding the location on the QPixMap of one of those items from the smaller scene.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Position in a QPixMap created from a QGraphicsScene

    Quote Originally Posted by stevel View Post
    Thanks for the reply. I don't think that you did understand my requirement correctly. I already have the two scene stuff working quite well, and it is exactly what I need.
    I'm not sure of that. I'd go for a single scene as well.

    The intent of the smaller scene is just to be a way to edit the QPixMap on the QGraphicsPixMapItem on the larger scene.
    According to me the "pixmap" should never have been a pixmap. It should be a composition of items, espcially if you want to treat it as something composed from items.

    A pixmap is a bitmap, it's composed from pixels not from objects. If your picture is to be a composition of objects then treat it as so.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Position in a QPixMap created from a QGraphicsScene

    No, you guys are not seeing the bigger picture. The way I am doing this makes sense, and really fits my project. Does anyone have a suggestion about my question?

    I will try again to explain what I am doing, which as I said, is working well, and doing exactly what I want.

    On the main QGraphicsScene, there are many types of Items. One of them is something called a block, which has an icon. The icon is represented by a QGraphicsPixMap. When you double click on a block, you can open up a window which allows you to edit it's icon. This window is also a QGraphicsScene. I cannot, and do not want to represent this as anything other then a QGraphicsPixMapItem, as there can potentially be thousands of blocks on the main window.

    For the most part, I don't care about the details of the Scene that was used to build the block, however, there are a couple of items on that scene that I do care about, and I need to save information about where those items are on the QPixMap when it is created. All I want to know is how to get the location (pos) of those items in the coordinates of the QPixMap, not the coordinates of the scene that made the QPixMap. Here is my latest try, which is not working yet.

    This code is in the GrapphicsItem whose position I care about, and I am trying to create a position point that is offset by the location of the created QPixMap, but I don't have the logic right obviously.

    Qt Code:
    1. QPointF point1, point2;
    2.  
    3. // JSL - Calculate the location of the position of the item on the icon
    4. point1 = mapToScene(pos());
    5. point2 = scene()->itemsBoundingRect().center();
    6. drawData.iconPos = point2 - point1;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Position in a QPixMap created from a QGraphicsScene

    Make the coordinates of the "pixmap scene" reflect those of the scene containing the pixmap. The easiest way to do it is to use QGraphicsItem::sceneBoundingRect(). Then pass the returned rect to QGraphicsScene::setSceneRect() of the newly created scene.

    Then all your calculations will be done in the same coordinate space.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Position in a QPixMap created from a QGraphicsScene

    wysota,

    Thanks for your help. I don't think I have fully managed to communicate what I am doing, because part of what you were saying really didn't apply, but your mention of the sceneBoundingRect function helped, because this code:

    Qt Code:
    1. QRectF rect;
    2. QPointF point1, point2;
    3.  
    4. // JSL - Calculate the location of the position of the item on the icon
    5. rect = sceneBoundingRect();
    6. point1 = rect.topLeft();
    7. point2 = scene()->itemsBoundingRect().topLeft();
    8. drawData.iconPos= point1 - point2;
    To copy to clipboard, switch view to plain text mode 

    seems to be doing the trick. Thanks again. I think I can move on from here.

Similar Threads

  1. Replies: 4
    Last Post: 16th March 2009, 10:08
  2. QPixmap display on QGraphicsScene
    By febil in forum Qt Programming
    Replies: 2
    Last Post: 26th February 2009, 10:27
  3. Saving the widget class as a QPixmap
    By zgulser in forum Newbie
    Replies: 11
    Last Post: 2nd February 2009, 10:48

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.