我有编程经验,但我还在学习,我决定创建一个 QVector 数组来存储一些 QGraphicsRectItem ,如下所示:
QVector<QGraphicsRectItem> *FreeLayer1;
FreeLayer1 = new QVector<QGraphicsRectItem>;
FreeLayer1->resize(10);
这是错误:
c:\QtSDK\Desktop\Qt\4.8.1\msvc2010\include\QtCore/qvector.h(532) : error C2248: 'QGraphicsRectItem::QGraphicsRectItem' : cannot access private member declared in class 'QGraphicsRectItem'
c:\qtsdk\desktop\qt\4.8.1\msvc2010\include\qtgui\qgraphicsitem.h(728) : see declration of 'QGraphicsRectItem::QGraphicsRectItem'
c:\qtsdk\desktop\qt\4.8.1\msvc2010\include\qtgui\qgraphicsitem.h(683) : see declaration of 'QGraphicsRectItem'
c:\QtSDK\Desktop\Qt\4.8.1\msvc2010\include\QtCore/qvector.h(473) : while compiling class template member function 'void QVector<T>::realloc(int,int)'
我知道这听起来可能很愚蠢或很容易做到,但我没有发现与我完全一样的错误,而且我对声明没有太多经验。我的问题是如何编写此代码以使用我的变量 FreeLayer1。我坚持使用QVector<>,就是不知道怎么声明。
感谢您的帮助! :)
最佳答案
你的声明很好,问题似乎是QGraphicsRectItem的默认构造函数是私有(private)的,所以你不能使用需要默认构造函数的QVector方法,比如QVector::resize。查看 QGraphicsRectItem 的文档,似乎也没有公共(public)复制构造函数或复制赋值运算符,因此 QGraphicsRectItem 不符合 QVector 的元素类型。您必须存储指向 QGraphicsRectItem 的指针:
QVector<QGraphicsRectItem*> FreeLayer1;
FreeLayers1.resize(10);
FreeLayers1[0] = new QGraphicsRectItem(/* ... */);
关于qt - QT中的QVector数组无法访问私有(private)成员错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12469142/