PDA

View Full Version : node_copy error, "QGraphicsRectItem is private"



Ishmael
5th October 2009, 00:53
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:

class numberItem : public QGraphicsRectItem
{
public:
numberItem(int cell_width);
private:
int cell_width;
};

// Constructor for numberItem
numberItem::numberItem(int w = 5)
: cell_width(w)
{
}

Second class:


class numberLine
{
public:
// IF I COMMENT OUT THIS LINE, IT COMPILES JUST FINE
QList<numberItem> all_number_items;

QList<int> all_x;
int num;
};

An the rest of the code:

numberLine get_num_line();

int main(int argc, char **argv){
QApplication app(argc, argv);

numberLine num_line;
num_line = get_num_line();

return app.exec();
}

numberLine get_num_line()
{
numberLine x;
return x;
}

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

scascio
5th October 2009, 08:45
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 :

QList<numberItem> all_number_items;
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 :

QList<numberItem*> all_number_items;
or to change you design and instead of deriving QGraphicsRectItem, use a member:

class numberItem {
public:
numberItem(int w) : cell_width(w), item(0){}
void setItem(QGraphicsRectItem* item);
QGraphicsRectItem* getItem();
private:
int cell_width;
QGraphicsRectItem * item;
};

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

Ishmael
6th October 2009, 01:09
Very helpful. Thanks a lot!