PDA

View Full Version : QGraphicsItem Subclassing



QbelcorT
18th November 2008, 08:05
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::paint(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

wysota
18th November 2008, 08:31
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).

QbelcorT
18th November 2008, 08:54
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.

wysota
18th November 2008, 10:43
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.

QbelcorT
19th November 2008, 01:06
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)
}