PDA

View Full Version : Using foreach on classes that are children of QGraphicsItem?



JesperWe
6th February 2010, 11:46
I am trying to apply the foreach keyword to my code, which does a lot of iterations over QLists containing objects of types inherited from QGraphicsItem.

So first problem was foreach requiring some methods (copy constructor, "=" operator) that I had not implemented in my child class.

So I implemented these. But then I run into problems with the assignment operator. AFAIK the child assignment operator should call the base class operator to ensure that base part of the object is properly assigned. But when I call it



myClass& operator=( const myClass& rightHand ) {
if ( &rightHand != this )
{
QGraphicsItem::operator =( rightHand );

// ...my assignment stuff here.
}
return *this;
}


the compiler tells me that

'QGraphicsItem& QGraphicsItem::operator=(const QGraphicsItem&)' is private

So what is the proper way to have a class derived from QGraphicsItem that I can use with foreach( myClass x, QList<myClass> y) ?

JohannesMunk
6th February 2010, 13:43
You should store pointers to your classes in your list. Then everything should work and you don't have to write a copy constructor.

franz
6th February 2010, 16:31
The reason why the copy constructor should be disabled is given in the documentation about the Qt Object Model (http://doc.trolltech.com/4.6/object.html)