Results 1 to 8 of 8

Thread: Subclassing QGraphicsItem to work as a layer

  1. #1
    Join Date
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Subclassing QGraphicsItem to work as a layer

    Hello everyone! I have a little problem: after subclassing QGraphicsItem in this way:
    Qt Code:
    1. #ifndef LAYER_H
    2. #define LAYER_H
    3. #include <QGraphicsItem>
    4. #include <QPointF>
    5. #include <QRectF>
    6. #include <QPolygonF>
    7. #include <QPainter>
    8. #include <QPainterPath>
    9. #include <QWidget>
    10. class Layer : public QGraphicsItem
    11. {
    12. public:
    13. Layer(int layerId = -1, QString layerName = "", QGraphicsItem *parent = 0);
    14. QRectF boundingRect() const
    15. {
    16. return QRectF(QPoint(0,0), QSizeF(10000.0, 10000.0)); //size can change
    17. }
    18. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    19. QWidget *widget)
    20. {
    21. painter->setRenderHint(QPainter::HighQualityAntialiasing);
    22. }
    23. int getId();
    24. bool setId(int layerId);
    25. QString getName();
    26. bool setName(QString layerName);
    27. signals:
    28. public slots:
    29. private:
    30. int layerId;
    31. QString layerName;
    32. protected:
    33. // void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
    34. };
    35. #endif // LAYER_H
    To copy to clipboard, switch view to plain text mode 

    The Layer class should work as a container for map tiles 256x256 px each. I got tiles displayed properly in a QGraphicsScene but when I add a Layer into it and then add same map tiles I get a blank screen. Debugging however shows that my parent Label contains correct number of tiles in it.

    Thanx in advance!
    Last edited by kefir; 9th March 2011 at 18:07.

  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: Subclassing QGraphicsItem to work as a layer

    Your drawing code doesn't make sense. Your boundingRect code is not any good either. Painting should be done in local coordinates defined by the boundingRect. It can't depend on the position of the item relative to its parent (which is what pos() represents).
    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
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclassing QGraphicsItem to work as a layer

    Sorry about these lines. I'll comment them. but in fact they did draw a line accoording to pos().
    boundinBox() is the thing that keeps on puzzling me. I don't quite get how to reimplement this function knowing that size of it may change... or does it mater in my case..

    I've updated the head message...

  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: Subclassing QGraphicsItem to work as a layer

    Quote Originally Posted by kefir View Post
    Sorry about these lines. I'll comment them. but in fact they did draw a line accoording to pos().
    Probably only because at the time of calling boundingRect() the value returned by pos() was (0,0).

    boundinBox() is the thing that keeps on puzzling me. I don't quite get how to reimplement this function knowing that size of it may change... or does it mater in my case..
    Nobody said the size returned by boundingRect() should be constant across the lifetime of the item. It's just important that when bounding rect changes, prepareGeometryChange() has to be called.
    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.


  5. #5
    Join Date
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclassing QGraphicsItem to work as a layer

    Ok... And so where may lay the source of my problem of child item invisibility?
    Should I reimplement itemChange() and put prepareGeometryChange() or should I trace a moment when a child item is added and call it just before then?

  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: Subclassing QGraphicsItem to work as a layer

    Quote Originally Posted by kefir View Post
    Ok... And so where may lay the source of my problem of child item invisibility?
    Probably in the bogus implementation of boundingRect of their parent.

    Should I reimplement itemChange() and put prepareGeometryChange() or should I trace a moment when a child item is added and call it just before then?
    You should start by reading the docs and understanding how Graphics View's coordinate system works. It will save you a lot of trouble. But in general your layer item should just be a QGraphicsItemGroup.
    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. The following user says thank you to wysota for this useful post:

    kefir (10th March 2011)

  8. #7
    Join Date
    Mar 2011
    Location
    Moscow, Russia
    Posts
    11
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Subclassing QGraphicsItem to work as a layer

    Thanks a lot!
    Any suggestions on literature... or just QGraphicsView, scene and item reference from Qt Creator?

  9. #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: Subclassing QGraphicsItem to work as a layer

    Reference manual is a good start.
    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. Why QGLFramebufferObject in QGraphicsItem don’t work?
    By Student3000 in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2010, 20:12
  2. QGraphicsItem::setToolTip does not work
    By Nadia in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2009, 19:54
  3. subclassing QGraphicsItem, QGraphicsPixmapItem
    By rogerholmes in forum Newbie
    Replies: 3
    Last Post: 26th August 2009, 23:12
  4. QGraphicsItem Subclassing
    By QbelcorT in forum Newbie
    Replies: 4
    Last Post: 19th November 2008, 01:06
  5. after subclassing will the inbuild signals work
    By babu198649 in forum Qt Programming
    Replies: 2
    Last Post: 26th November 2007, 15:10

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.