Results 1 to 7 of 7

Thread: How to put scaled QGraphicsItems next to eachother

  1. #1
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to put scaled QGraphicsItems next to eachother

    Hi all.

    I'm having an issue with my QGraphicsItems. I want to put multiple QGraphicsItems next to eachother. What would be the best way to do that?

    I used this code, which only works when the items are not transformed. But what should I use when the items are infact transformed? Because boundingRect() doesn't take the transformation into account.

    Qt Code:
    1. for (int i = 0; i < itemList.size(); i++) {
    2. itemList[i]->setTransformationMode(Qt::SmoothTransformation);
    3. if (i > 0) itemList[i]->setPos(itemList[i-1]->x()+itemList[i-1]->boundingRect().width(), 0);
    4. itemList[i]->setPos(itemList[i]->x(), (200-itemList[i]->boundingRect().height())/2.0);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I hope someone here can help me.... 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: How to put scaled QGraphicsItems next to eachother

    All transformations are done relative to the (0,0) point, so if you place this point on the middle of the shared side of those items, they should be transformed correctly. You can also put both of them into an item group and transform the group and not individual items.

  3. #3
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to put scaled QGraphicsItems next to eachother

    Thanks for your answer, but I believe you are misunderstanding me.
    Scaling works and looks fine.
    The problem is that when I scale a QGraphicsItem,
    that I can't find out where the next QGraphicsItem has to be located,
    because QGraphicsItem::boundingRect().width() will return the width of the QGraphicsItem _without_ the scaling, so if I do a QGraphicsItem::scale(0.5, 0.5) on my first QGraphicsItem and I then call this for my second QGraphicsItem:

    secondQGraphicsItem->setPos(firstQGraphicsItem->x()+firstQGraphicsItem->boundingRect().width(), 0);

    the second QGraphicsItem will not be directly next to the first one, there will be a huge space in between them. How should I take the transformation into account?

  4. #4
    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: How to put scaled QGraphicsItems next to eachother

    Use QGraphicsItem::mapTo*() family of methods to map to parent or the scene.

  5. #5
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to put scaled QGraphicsItems next to eachother

    Quote Originally Posted by wysota View Post
    Use QGraphicsItem::mapTo*() family of methods to map to parent or the scene.
    I'm sorry, but I don't understand how that would help me. Could you explain a bit more how I would have to use it?

    I need to know the real width() of the QGraphicsItem with the transformation taken into account. How should I use one of the mapTo*() functions to achieve that, or to achieve something similar? As far as I am aware, the mapTo*() functions also don't know about the transformation.

  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: How to put scaled QGraphicsItems next to eachother

    Quote Originally Posted by profoX View Post
    I need to know the real width() of the QGraphicsItem with the transformation taken into account. How should I use one of the mapTo*() functions to achieve that, or to achieve something similar?
    Map the rectangle of the item to the scene (world) coordinate space. Or simply use QGraphicsItem::sceneBoundingRect(), it does exactly the same.

  7. The following user says thank you to wysota for this useful post:

    profoX (3rd June 2008)

  8. #7
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to put scaled QGraphicsItems next to eachother

    Quote Originally Posted by wysota View Post
    Map the rectangle of the item to the scene (world) coordinate space. Or simply use QGraphicsItem::sceneBoundingRect(), it does exactly the same.
    Ok. Thanks for the extra information buddy! I'm now starting to understand how I have to do it This is the result so far:

    Pretty satisfying for what I want to use it for.


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.