Results 1 to 3 of 3

Thread: Qt4/Mac: funny problem with QGraphicsItems

  1. #1
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Qt4/Mac: funny problem with QGraphicsItems

    Hi all,

    I'm experiencing a very funny problem with the Graphics View Framework (Mac OS, Qt 4.3.2, G++ 4).

    I've created a very simple class, called Obstacle, that inherits from QGraphicsItem:

    Qt Code:
    1. #ifndef OBSTACLE_H
    2. #define OBSTACLE_H
    3.  
    4. #include <QGraphicsItem>
    5.  
    6. class Obstacle : public QObject, public QGraphicsItem {
    7.  
    8. Q_OBJECT
    9.  
    10. public:
    11.  
    12. // Constructor
    13. Obstacle(int width, int height, bool movable);
    14.  
    15. // Size
    16. int obstacle_width, obstacle_height;
    17.  
    18. // itemChange() function
    19. QVariant itemChange(GraphicsItemChange change, const QVariant & value);
    20.  
    21. // Shape
    22. QRectF boundingRect() const;
    23. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    24.  
    25. // Tooltip
    26. QString str_obstacle_tooltip;
    27.  
    28. signals:
    29. void dragged();
    30.  
    31. };
    32.  
    33. #endif
    To copy to clipboard, switch view to plain text mode 

    Since I need to use a lot of objects belonging to this class, I've chosen to create a couple of dynamically allocated vectors, defined as:

    Qt Code:
    1. Obstacle *obstacles_A[], *obstacles_B[];
    To copy to clipboard, switch view to plain text mode 

    Then, I create the various instances using the "new" command and I add all the object to my scene:

    Qt Code:
    1. obstacles_A[0] = new Obstacle(130, 82, true);
    2. obstacles_A[1] = new Obstacle(130, 82, true);
    3. obstacles_A[2] = new Obstacle(95, 190, true);
    4. obstacles_A[3] = new Obstacle(55, 230, true);
    5. ...
    6. obstacles_A[12] = new Obstacle(49, 38, true);
    7.  
    8. graphScene_Scene->addItem(obstacles_A[0]);
    9. graphScene_Scene->addItem(obstacles_A[1]);
    10. graphScene_Scene->addItem(obstacles_A[2]);
    11. graphScene_Scene->addItem(obstacles_A[3]);
    12. ...
    13. graphScene_Scene->addItem(obstacles_A[12]);
    14.  
    15. obstacles_A[0]->setPos(180,44);
    16. obstacles_A[1]->setPos(180,150);
    17. obstacles_A[2]->setPos(408,38);
    18. obstacles_A[3]->setPos(643,17);
    19. ...
    20. obstacles_A[12]->setPos(275,564);
    To copy to clipboard, switch view to plain text mode 

    Everything works fine in this way. Except if I try to create another object (the number 13). In this case, my application compiles correctly but I get an error during the linking.

    This is why I'm using also another vector (obstacles_B). Using this second vector, I'm able to create more than 13 obstacles, but the amazing thing is that, when I add to the scene the first objects belonging to the second vector, the first object belonging to the first vector disappears from the scene! If I try with a second obstacle_b object, the second obstacle_a disappears, and so on.

    Even more strange, it seems that the objects of the second vector "inherits" some properties from the disappearing objects: if I select (since the obstacles are selectable) one of the obstacles_b, it appears to my application as if I'd selected an object of the obstacles_a class.

    Anyone could please help me to clarify this behaviour?

    Many thanks,
    Fabio Ruini

  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: Qt4/Mac: funny problem with QGraphicsItems

    Qt Code:
    1. Obstacle *obstacles_A[]
    To copy to clipboard, switch view to plain text mode 
    This is incorrect - it doesn't carry the dimensions of the array and you probably trash your stack or something like that.

    You should definitely use a vector or a list:
    Qt Code:
    1. QList<Obstacle*> obstacles;
    2. obstacles << new Obstacle(...);
    3. //or
    4. obstacles.resize(10);
    5. obstacles[7] = new Obstacle(...);
    To copy to clipboard, switch view to plain text mode 

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

    fabietto (22nd November 2007)

  4. #3
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt4/Mac: funny problem with QGraphicsItems

    Hey Wisota,

    thank you very much. You were right, the problem wasn't related to the Qt, but just to C++!

    Sorry for the late reply, but I've experienced few login problems during the last days.

    Thanks again,
    Fabio Ruini

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.