Results 1 to 3 of 3

Thread: node_copy error, "QGraphicsRectItem is private"

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default node_copy error, "QGraphicsRectItem is private"

    I have two classes: "numberItem" and "numberLine".

    The first is derived from QGraphicsRectItem. It works just fine by itself.

    The other one is not really a class - more of a C-style struct - in the sense that all I would like to do is use it as a container for a bunch of related information. Among other things, I want it to contain an array of pointers to the first class.

    Below is a simplified version of my code. Can anyone tell me what I'm doing wrong? Thanks so much!

    First class:
    Qt Code:
    1. class numberItem : public QGraphicsRectItem
    2. {
    3. public:
    4. numberItem(int cell_width);
    5. private:
    6. int cell_width;
    7. };
    8.  
    9. // Constructor for numberItem
    10. numberItem::numberItem(int w = 5)
    11. : cell_width(w)
    12. {
    13. }
    To copy to clipboard, switch view to plain text mode 

    Second class:
    Qt Code:
    1. class numberLine
    2. {
    3. public:
    4. // IF I COMMENT OUT THIS LINE, IT COMPILES JUST FINE
    5. QList<numberItem> all_number_items;
    6.  
    7. QList<int> all_x;
    8. int num;
    9. };
    To copy to clipboard, switch view to plain text mode 

    An the rest of the code:
    Qt Code:
    1. numberLine get_num_line();
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5.  
    6. numberLine num_line;
    7. num_line = get_num_line();
    8.  
    9. return app.exec();
    10. }
    11.  
    12. numberLine get_num_line()
    13. {
    14. numberLine x;
    15. return x;
    16. }
    To copy to clipboard, switch view to plain text mode 

    When I try to compile, I get a whole litany of unintelligible errors:
    instantiated from `void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = numberItem]'
    instantiated from `void QList<T>::detach_helper() [with T = numberItem]'
    instantiated from `QList<T>::QList(const QList<T>&) [with T = numberItem]'
    instantiated from here
    error: `QGraphicsRectItem::QGraphicsRectItem(const QGraphicsRectItem&)' is private

  2. #2
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: node_copy error, "QGraphicsRectItem is private"

    By default, you are not allowed to copy QGraphicsItem by default, except if you do so in derived class. The copy constructor is private as the compiler says.

    In :
    Qt Code:
    1. QList<numberItem> all_number_items;
    To copy to clipboard, switch view to plain text mode 
    The copy constructor is implicitly called by your container and that's why it compiles when you comment it.

    The solution is to use a container of pointers that manages your derived items :
    Qt Code:
    1. QList<numberItem*> all_number_items;
    To copy to clipboard, switch view to plain text mode 
    or to change you design and instead of deriving QGraphicsRectItem, use a member:
    Qt Code:
    1. class numberItem {
    2. public:
    3. numberItem(int w) : cell_width(w), item(0){}
    4. void setItem(QGraphicsRectItem* item);
    5. QGraphicsRectItem* getItem();
    6. private:
    7. int cell_width;
    8. };
    To copy to clipboard, switch view to plain text mode 

    Remember that items added in the scene are owned by the QGraphicsScene, that will delete them when removed.

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

    Ishmael (6th October 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: node_copy error, "QGraphicsRectItem is private"

    Very helpful. Thanks a lot!

Tags for this Thread

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.