Results 1 to 12 of 12

Thread: QGraphicsItemGroup - store informations about children.

  1. #1
    Join Date
    Oct 2010
    Location
    Cracow,Bielsko-Biała/Polska
    Posts
    15
    Thanks
    3
    Platforms
    Unix/X11 Windows

    Default QGraphicsItemGroup - store informations about children.

    I have QGraphicsItemGroup object with children.
    I want to save this group to file, then retrives informations about group from file.

    What kind of information about child items, should I save ?
    Beside of pos(), shape().. should I save tranformations/parent transformations or bounding rect too ?

    Thanks for reply.

  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: QGraphicsItemGroup - store informations about children.

    You only need pos(), the type of item and a local transformation matrix of the item to be able to recreate the item back. Of course besides all the custom attributes an item may have (colour, brush, etc.).
    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. The following user says thank you to wysota for this useful post:

    Leszek (25th October 2010)

  4. #3
    Join Date
    Oct 2010
    Location
    Cracow,Bielsko-Biała/Polska
    Posts
    15
    Thanks
    3
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItemGroup - store informations about children.

    Thanks for reply.

    So, I don`t have to store shape() of child elems ?

  5. #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: QGraphicsItemGroup - store informations about children.

    No, what do you need it for? The shape is strictly related to the item type, you can always recalculate it knowing positions and item types. There is a pitfall here - you might think that you need to store shape or at least boundingRect for items such as QGraphicsRectItem because the item's bounding rect can be different depending on the rectangle it represents. But in reality you don't need the boundingRect, you only need the rectangle that defines it (or the ellipse of the ellipse item or the two end points of the line item, etc.).
    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.


  6. #5
    Join Date
    Oct 2010
    Location
    Cracow,Bielsko-Biała/Polska
    Posts
    15
    Thanks
    3
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItemGroup - store informations about children.

    Thanks, but propably description of my problem is wrong.

    I`ve 3 elems. For eg. QGraphicsPathItems. Because of, that objects are grouped/ungrouped so I need to have that objects in one group.

    So I create QGraphicsItemGroup object. Now, this group contains 3 objects (QGraphicsPathItems).

    Storing to DB.
    Group is stored to dataStream like below:

    Qt Code:
    1. ds << childItems().count();
    2.  
    3. BOOST_FOREACH(QGraphicsItem* item, childItems())
    4. {
    5. GraphicType typeChild = (GraphicType)item->type();
    6. switch(typeChild)
    7. {
    8. case eShapeElem:
    9. {
    10. ShapeElem* shapeElem = qgraphicsitem_cast<ShapeElem*>(item);
    11. ds << shapeElem;
    12. //of course proper operators were created
    13. // QDataStream &operator<<(QDataStream&, const ShapeElem*);
    14. // QDataStream &operator>>(QDataStream&, ShapeElem*);
    15. break;
    16. }
    17. .. other types
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Group is read from dataStream like below:

    Qt Code:
    1. int countChildren;
    2.  
    3. ds >> countChildren;
    4. for(int iIdx = 0; iIdx < countChildren....) {
    5.  
    6. ds >> typeChild;
    7. switch(typeChild)
    8. {
    9. case eShapeElem:
    10. {
    11. item = new ShapeElem();
    12. dataStream >> (ShapeElem*)item;
    13. lstRetrivedChildren.add(item);
    14. break;
    15. }
    16. }
    17. }
    18. BOOST_FOREACH(QGraphicsItem* item, lstRetrivedChildren)
    19. addToGroup(item);
    To copy to clipboard, switch view to plain text mode 

    Storing ShapeElem to stream:

    Qt Code:
    1.  
    2. ds << (qint32)type();
    3. ds << path();
    To copy to clipboard, switch view to plain text mode 

    Reading ShapeElem from stream:

    Qt Code:
    1.  
    2. ds >> path;
    3.  
    4. setPath(path);
    To copy to clipboard, switch view to plain text mode 

    If I don`t save path(rect or other) of children, How I create them after load file?

    Thanks!
    Last edited by Leszek; 25th October 2010 at 17:54.

  7. #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: QGraphicsItemGroup - store informations about children.

    "path" is one of the "extra attributes" you need to store of course. When talking about "shape" of graphics items I understand QGraphicsItem::shape().

    By the way, your serialization solution is a bit poorly "object oriented", try something like this:

    Qt Code:
    1. QDataStream ds(...);
    2. ds << scene->items().count();
    3. foreach(QGraphicsItem* item, scene->items()) {
    4. MyBaseItemClass *myItem = qgraphicsitem_cast<MyBaseItemClass*>(item);
    5. if(!myItem) continue;
    6. myItem->serialize(ds); // define serialize() for each of your item classes
    7. }
    To copy to clipboard, switch view to plain text mode 

    No need for any switches and such. Alternatively define streaming operators for item classes and call:
    Qt Code:
    1. ds << *item;
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. ds >> *item;
    To copy to clipboard, switch view to plain text mode 
    respectively.
    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.


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

    Leszek (25th October 2010)

  9. #7
    Join Date
    Oct 2010
    Location
    Cracow,Bielsko-Biała/Polska
    Posts
    15
    Thanks
    3
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItemGroup - store informations about children.

    Thanks! Load/Save group is working very well now.

    Because of a I want to add this group to tree and shows it like a icon, I have to "draw" this group as QPixmap..

    Not working. Child items have incorect position...
    Code like below. (QGraphicsItemGroup isn`t added to Scene).

    Qt Code:
    1. QGraphicsItemGroup lodedGroupFromFile; //not added to scene
    2.  
    3. QPixmap pixmap(45, 45);
    4. QPainter painter(&pixmap);
    5.  
    6.  
    7. BOOST_FOREACH(QGraphicsItem* item, lodedGroupFromFile->children()) {
    8. item->paint(&painter, &opt);
    9. }
    10.  
    11. scene->addPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

    QPixmap is added to Scene, but children of group are draw incorectly(positions are incorect).
    Last edited by Leszek; 25th October 2010 at 19:44.

  10. #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: QGraphicsItemGroup - store informations about children.

    Drawing items is not as easy. Some transformations need to be done to the painter to draw the items correctly. It's best to rely on Graphics View here - call QGraphicsScene::render() with the rect containing the bounding rect of the item and all its children. To make sure no other items are drawn, hide them prior to rendering what you need.
    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.


  11. #9
    Join Date
    Oct 2010
    Location
    Cracow,Bielsko-Biała/Polska
    Posts
    15
    Thanks
    3
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItemGroup - store informations about children.

    Thanks wysota!!!.
    It was very helpfully for me. It is working now.

    Group from scene is saved to file. This file contains informations about QPixmap involves with this group. So durring loading, content of object is loaded with additionally info about QPixmap. Then I use it to render object as icon.

    I were confused, after read informations about differents between QImage and QPixmap,
    Now durring saving object to file, informations about QPixmap is stored too.
    Maybe better solution is storing QImage info instead of QPixmap?

    Can I save object from scene as a icon with transparent background ?

  12. #10
    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: QGraphicsItemGroup - store informations about children.

    Quote Originally Posted by Leszek View Post
    Maybe better solution is storing QImage info instead of QPixmap?
    You don't store a pixmap to a file. When you save a pixmap, its image representation is saved instead.

    Can I save object from scene as a icon with transparent background ?
    If your scene has transparent background and the pixmap you draw on is transparent then after calling QGraphicsScene::render() you should have a pixmap with transparent background.
    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.


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

    Leszek (27th October 2010)

  14. #11
    Join Date
    Oct 2010
    Location
    Cracow,Bielsko-Biała/Polska
    Posts
    15
    Thanks
    3
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItemGroup - store informations about children.

    Thanks !!! Now I understand it.

    Last question in this thread(I promise )

    How can I make something like "clone" of QGraphicsItem ?
    Copy contructor is private...
    By "clone" I understand exactly the same object(copy of source object), but not in memmory of course.

  15. #12
    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: QGraphicsItemGroup - store informations about children.

    You have to implement that yourself by copying all relevant information from the original to the copy. It's easiest to do that with serialization/deserialization which works essentially like copy & paste.
    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.


Similar Threads

  1. QSqlRecord...retrieving informations
    By lord_shadow in forum Qt Programming
    Replies: 3
    Last Post: 5th March 2009, 12:54
  2. how to get error or informations from a exe under dos?
    By raphaelf in forum Qt Programming
    Replies: 9
    Last Post: 20th June 2008, 10:17
  3. display informations of QDirModel on QDialog
    By hbill in forum Qt Programming
    Replies: 1
    Last Post: 10th April 2008, 21:36
  4. Add two QGraphicsItemGroup
    By ashishsaryar in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2008, 18:00
  5. RTQI : Run-Time Qt Informations ?
    By fullmetalcoder in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2006, 12:01

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.