Results 1 to 5 of 5

Thread: QGraphicsItem Subclassing

  1. #1
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default QGraphicsItem Subclassing

    Hi,
    This may be a basic C++ question, not sure.
    I have a QGraphicsRectItem class, call it CIRCLE which implements painting, item events and such.
    Now I want to make another QGraphicsItem called SQUARE. In this item SQUARE, it contains many CIRCLE items.
    The SQUARE class only needs to create the circle items. I want the scene to add the SQUARE item, instead of creating many circle items in my scene view. ( just trying to clean up the code)
    I get this error when compiling
    :294: error: cannot declare variable 'SQUARE' to be of abstract type 'SQUARE'
    :84: note: because the following virtual functions are pure within 'SQUARE'

    /usr/local/Trolltech/QtEmbedded-4.4.1-arm/include/QtGui/qgraphicsitem.h:224: note: virtual QRectF QGraphicsItem::boundingRect() const
    /usr/local/Trolltech/QtEmbedded-4.4.1-arm/include/QtGui/qgraphicsitem.h:243: note: virtual void QGraphicsItem:aint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)

    If I implement these functions in the SQUARE class then there is no error. However, I do not need to paint events for SQUARE. The painting is handled by CIRCLE class.
    Please help.

    Thank you
    Last edited by QbelcorT; 18th November 2008 at 09:12.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem Subclassing

    I suggest you simply use QGraphicsItemGroup instead of your SQUARE implementation. If you want to stay with your current approach, you have to implement those methods, even if their implementation is empty (boundingRect returning [0,0] and empty paint).

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

    QbelcorT (19th November 2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QGraphicsItem Subclassing

    thanks for your reply.
    I don't think this will work for my intentions.I only need to add one item to the scene, the others are parented off the first one. Only the first one is added to the scene.
    For example: This SQUARE item is a window, the CIRCLES are the buttons/icons on that window. The circles have their own implementation to accept mouse buttons, painting, and effects when the button is pressed.
    I would like to have a separate file 'window1.cpp' which has the SQUARE item. The main scene/view file is getting quite large and confusing with all the CIRCLE items I have. I would like the window properties in a separate file.
    The scene would just simplying addItem->SQUARE. Without the overhead of the window implementation.
    Thanks.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem Subclassing

    Quote Originally Posted by QbelcorT View Post
    thanks for your reply.
    I don't think this will work for my intentions.I only need to add one item to the scene, the others are parented off the first one. Only the first one is added to the scene.
    That's exactly what the item group is for.

    For example: This SQUARE item is a window, the CIRCLES are the buttons/icons on that window. The circles have their own implementation to accept mouse buttons, painting, and effects when the button is pressed.
    Is the "window" somehow "visible"? Does it draw anything? Does it have dimensions? If so, then you need to use a regular item with boundingRect() and paint() implementation. If not, you can safely use an item group.

    Remember you can subclass the group item and add child items there in its constructor. Then you'd simply add such a group to your scene and all items would be created by the group item.

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

    QbelcorT (19th November 2008)

  7. #5
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QGraphicsItem Subclassing

    Great, thanks it worked out. I haven't used QGraphicsItemGroup before so it took some trial and errors but I got it working. If I addToGroup all the buttons, then the buttons do not work, I just need to addToGroup the window, since the buttons are children.

    class MyWindow : public QObject, public QGraphicsItemGroup
    ...

    MyWindow::MyWindow(QGraphicsItem *parent)
    :QGraphicsItemGroup(parent)
    {
    ... main window created (window), setParentItem(parent)
    ... many button items created (buttons), setParentItem(window)
    addToGroup(window)
    }

Similar Threads

  1. QGraphicsItem doesn't inherit QObject?
    By xyzt in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2011, 15:59
  2. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 11:31
  3. QGraphicsItem force boundingRect
    By bunjee in forum Qt Programming
    Replies: 0
    Last Post: 27th September 2008, 17:49
  4. Problem with rotation of QGraphicsItem
    By ashishsaryar in forum Qt Programming
    Replies: 2
    Last Post: 10th July 2008, 16:03
  5. Snap-to grid on QGraphicsItem
    By hardgeus in forum Qt Programming
    Replies: 9
    Last Post: 28th April 2008, 17:22

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.