The Ultimate Qt Community site
Home News Forum Wiki Contest FAQ Links

Go Back   Qt Centre Forum > Qt > Newbie

Newbie Newbie to Qt? Ask your questions here.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 18th November 2008, 08:05
QbelcorT QbelcorT is offline
Intermediate user
 
Join Date: Jul 2008
Qt products used: Qt4 , Qtopia
Qt platforms used: Unix/X11
Posts: 117
Thanks: 9
Thanked 13 Times in 12 Posts
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 08:12.
Reply With Quote
  #2  
Old 18th November 2008, 08:31
wysota wysota is offline
Guru
 
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used: Qt3 , Qt4 , Qt/Embedded
Qt platforms used: Unix/X11 , Windows
Posts: 14,363
Thanks: 3
Thanked 2,076 Times in 2,016 Posts
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).
Reply With Quote
The following user says thank you to wysota for this useful post:
QbelcorT (19th November 2008)
  #3  
Old 18th November 2008, 08:54
QbelcorT QbelcorT is offline
Intermediate user
 
Join Date: Jul 2008
Qt products used: Qt4 , Qtopia
Qt platforms used: Unix/X11
Posts: 117
Thanks: 9
Thanked 13 Times in 12 Posts
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.
Reply With Quote
  #4  
Old 18th November 2008, 10:43
wysota wysota is offline
Guru
 
Join Date: Jan 2006
Location: Warsaw, Poland
Qt products used: Qt3 , Qt4 , Qt/Embedded
Qt platforms used: Unix/X11 , Windows
Posts: 14,363
Thanks: 3
Thanked 2,076 Times in 2,016 Posts
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.

Quote:
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.
Reply With Quote
The following user says thank you to wysota for this useful post:
QbelcorT (19th November 2008)
  #5  
Old 19th November 2008, 01:06
QbelcorT QbelcorT is offline
Intermediate user
 
Join Date: Jul 2008
Qt products used: Qt4 , Qtopia
Qt platforms used: Unix/X11
Posts: 117
Thanks: 9
Thanked 13 Times in 12 Posts
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)
}
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
QGraphicsItem force boundingRect bunjee Qt Programming 0 27th September 2008 16:49
Problem with rotation of QGraphicsItem ashishsaryar Qt Programming 2 10th July 2008 15:03
Snap-to grid on QGraphicsItem hardgeus Qt Programming 9 28th April 2008 16:22
QGraphicsItem doesn't inherit QObject? xyzt Qt Programming 1 22nd March 2008 21:35
destruction of QGraphicsItem killkolor Qt Programming 1 4th December 2006 21:55


All times are GMT +1. The time now is 02:26.


Powered by vBulletin Version 3.7.4 Copyright ©2000 - 2009, Jelsoft Enterprises Ltd., vRewrite 1.5 SEOed URLs completed by Tech Help Forum and Chalo Na.
© 2006–2009 Qt Centre - The Ultimate Qt Community site
Nokia, Qt and their respective logos are trademarks of Nokia Corporation in Finland and/or other countries worldwide.