迭代QGraphicsItem项时构建错误

时间:2015-03-09 14:35:40

标签: c++ qt for-loop graphics

我已将我的类Item更改为继承自QGraphicsItem,并突然遇到问题......

class Item : public QGraphicsItem
{
    Item();
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
               QWidget *widget);
};

class CollectionView : public QGraphicsView
{
    Q_OBJECT    
public:
    CollectionView(QWidget *parent = 0);    
    QList<Item> *m_items;
};

class ShapeView : public QGraphicsView
{
    Q_OBJECT    
public:
    ShapeView(QWidget *parent = 0);
    CollectionView *collectionView;    
private:
    void doWork();
};

void ShapeView::doWork()
{
    outputView1->m_items->clear();
    foreach (Item item, collectionView->m_items)
   // for(int i = 0; i< collectionView->m_items->size(); i++)
    {
       // Item item = collectionView->m_items->at(i);    
       ...
    }
}

for循环或foreach,给我错误(在继承QGraphicsItem之前没问题):

These errors if I use foreach:
'QList<Item>*' is not a class, struct, or union type
'class QForeachContainer<QList<Item>*>' has no member named 'i'
'class QForeachContainer<QList<Item>*>' has no member named 'e'
'class QForeachContainer<QList<Item>*>' has no member named 'i'
'class QForeachContainer<QList<Item>*>' has no member named 'i'
using invalid field 'QForeachContainer<T>::i'
request for member 'begin' in '((QForeachContainer<QList<Item>*>*)this)->QForeachContainer<QList<Item>*>::c', which is of non-class type 'QList<Item>* const'
using invalid field 'QForeachContainer<T>::e'
...
In copy constructor 'Item::Item(const Item&)':
'QGraphicsItem::QGraphicsItem(const QGraphicsItem&)' is private
within this context
In member function 'void ShapeView::doWork()':
synthesized method 'Item::Item(const Item&)' first required here
In member function 'Item& Item::operator=(const Item&)':
'QGraphicsItem& QGraphicsItem::operator=(const QGraphicsItem&)' is private

These errors if I use for loop: I only get:
In copy constructor 'Item::Item(const Item&)':
'QGraphicsItem::QGraphicsItem(const QGraphicsItem&)' is private
within this context
In member function 'void ShapeView::doWork()':
synthesized method 'Item::Item(const Item&)' first required here
In member function 'Item& Item::operator=(const Item&)':
'QGraphicsItem& QGraphicsItem::operator=(const QGraphicsItem&)' is private

我做错了什么,这些错误意味着什么,我该如何解决?

2 个答案:

答案 0 :(得分:1)

这是相关部分:

In copy constructor 'Item::Item(const Item&)':
'QGraphicsItem::QGraphicsItem(const QGraphicsItem&)' is private
within this context

您继承自QGraphicsItem并且您需要是可复制的,但QGraphicsItem isn't copyable

Item中的foreach需要ShapeView::doWork()的复制构造函数,因此您必须以不同方式编写该函数并明确删除您的复制构造函数,或者找到一种方法来定义一个有效的cctor。 operator=也是如此。

答案 1 :(得分:0)

有效地,tux3是正确的,您只需要重新定义 cctor operator = 。 没有什么是不可侵犯的,创建自己继承的QGraphicsItem类 的QGraphicsItem。

file.h

 #include <QGraphicsItem>
    class MyGraphicsItem : public QGraphicsItem{
    MyGraphicsItem();//constructor
    MyGraphicsItem(const MyGraphicsItem& );//cctor
    MyGraphicsItem& operator=(const MyGraphicsItem& );//operator=
    }

file.cpp

  #include "file.h"
    //constructor
    MyGraphicsItem::MyGraphicsItem() : QGraphicsItem(){}
    //cctor
    MyGraphicsItem::MyGraphicsItem(const MyGraphicsItem& other){}
    //operator=
    MyGraphicsItem& MyGraphicsItem::operator=(const MyGraphicsItem& other){}

您不需要编写 cctor operator = 的正文。这种重新定义的唯一目的是能够处理今后 - 类QGraphicsItem的 cctor operator =

不要害怕这些签名,对于 cctor ,您可以选择4种可能性:

MyClass( const MyClass& other );
MyClass( MyClass& other );
MyClass( volatile const MyClass& other );
MyClass( volatile MyClass& other );

对于 operator = ,你可以选择这些可能性:

MyClass& operator=( const MyClass& rhs );
MyClass& operator=( MyClass& rhs );
MyClass& operator=( MyClass rhs );
const MyClass& operator=( const MyClass& rhs );
const MyClass& operator=( MyClass& rhs );
const MyClass& operator=( MyClass rhs );
MyClass operator=( const MyClass& rhs );
MyClass operator=( MyClass& rhs );
MyClass operator=( MyClass rhs );