Results 1 to 3 of 3

Thread: Using foreach on classes that are children of QGraphicsItem?

  1. #1
    Join Date
    Dec 2009
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Using foreach on classes that are children of QGraphicsItem?

    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

    Qt Code:
    1. myClass& operator=( const myClass& rightHand ) {
    2. if ( &rightHand != this )
    3. {
    4. QGraphicsItem::operator =( rightHand );
    5.  
    6. // ...my assignment stuff here.
    7. }
    8. return *this;
    9. }
    To copy to clipboard, switch view to plain text mode 

    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) ?

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using foreach on classes that are children of QGraphicsItem?

    You should store pointers to your classes in your list. Then everything should work and you don't have to write a copy constructor.

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using foreach on classes that are children of QGraphicsItem?

    The reason why the copy constructor should be disabled is given in the documentation about the Qt Object Model
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

Similar Threads

  1. Only get children
    By wirasto in forum Newbie
    Replies: 1
    Last Post: 24th January 2010, 17:15
  2. Foreach performance
    By jano_alex_es in forum General Programming
    Replies: 2
    Last Post: 17th November 2009, 13:26
  3. Casting QGraphicsItem child from QGraphicsItem
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2008, 15:37
  4. Replies: 2
    Last Post: 28th June 2008, 16:31
  5. children()
    By vermarajeev in forum Qt Programming
    Replies: 2
    Last Post: 14th May 2007, 15:15

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.