Results 1 to 5 of 5

Thread: Mapping QGraphicsItem objects

  1. #1
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Mapping QGraphicsItem objects

    Dear All!
    Qt Code:
    1. QGraphicsTextItem *scrItem = new QGraphicsTextItem("WELCOME MY SIR");
    2. scrItem->setDocument(txt->document());
    3. QDir::setCurrent(QDir::currentPath ()+"/media");
    4.  
    5. QPixmap pico("rose.jpg");
    6. pico=pico.scaled ( t.Font.pointSize(),t.Font.pointSize(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
    7.  
    8. QGraphicsItem *ship=new MyPixmapItem(pico,0,1);
    9.  
    10.  
    11. scrItem->setFont(t.Font);
    12.  
    13. gpItem->addToGroup (ship);
    14. gpItem->addToGroup (scrItem);
    15. gpItem->addToGroup (scrItem1);
    16.  
    17.  
    18. QPointF m(scrItem->mapToItem (ship,QPointF(ship->boundingRect().width(),0)));
    19. qDebug()<<"Before\tText Item POS = "<<scrItem->scenePos()<<" Image Item POS = "<<ship->scenePos();
    20.  
    21. scrItem->changeGeometry();
    22. QPointF sf=scrItem->mapFromScene(scrItem->mapToScene(m));
    23. qDebug()<<sf;
    24. qDebug()<<"After\tText Item POS = "<<scrItem->scenePos()<<" Image Item POS = "<<ship->scenePos()<<"\n";
    25. view=new QGraphicsView(scene,btgScrollText);
    26. scene->addItem(gpItem);
    27. animation->setItem(gpItem);
    To copy to clipboard, switch view to plain text mode 
    I want to put scrItem after ship object. How to map this.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    37
    Thanked 53 Times in 40 Posts

    Default Re: Mapping QGraphicsItem objects

    Qt Code:
    1. ...
    2. gpItem->addToGroup (ship);
    3. gpItem->addToGroup (scrItem);
    4. gpItem->addToGroup (scrItem1);
    5.  
    6. QPointF right(ship->boundingRect().right(), 0);
    7.  
    8. scrItem->setPos(ship->mapToParent(right)); //this should do the trick.
    9. ...
    To copy to clipboard, switch view to plain text mode 

    But there is a subtle problem here. Moving the items individually after inserting into a group doesn't update group's geometry. So there are two solutions for this
    1) Add all the items to scene first, position them as needed and then add to group.
    2) Otherwise preserve what you are doing, but after that add a dummy item and remove it from scene to update group's geometry.

    I think there is fault in your code. You are trying to map points to/from scene before adding those items to scene.

    Hope this helps
    Last edited by Gopala Krishna; 3rd February 2008 at 15:04.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: Mapping QGraphicsItem objects

    Dear Gopala Krishna!
    Thanks! for your valuable response.
    Except setPos(), is any function exists to set position of items in a scene & view.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

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

    Default Re: Mapping QGraphicsItem objects

    and why would you need it ? to set positions of objects in Scene ? setPos is meant for that.

    Also if you are confused between the mapToParent,mapToscene, etc,,,,, use item->scenePos() function.
    rest gopal has given correct answer i guess for the details u gave.

  5. #5
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    37
    Thanked 53 Times in 40 Posts

    Default Re: Mapping QGraphicsItem objects

    Quote Originally Posted by ashukla View Post
    Dear Gopala Krishna!
    Thanks! for your valuable response.
    Except setPos(), is any function exists to set position of items in a scene & view.
    Hmm, i guess no.. Note that setPos sets item's position in parent's coordinates. Only if it doesn't have parent setPos sets item's pos in scene coordinates.
    Source : QGraphicsItem::setPos
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

Similar Threads

  1. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31

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
  •  
Qt is a trademark of The Qt Company.