PDA

View Full Version : Whats wrong with QList<QGraphicsItem>



rubenvb
21st January 2010, 17:39
Under gcc:


#include <QtGui>
#include <QtCore>

class test
{
public:
test()
: list( QList<QGraphicsLineItem>() )
{ }

private:
QList<QGraphicsLineItem> list;
};

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

test t;

return app.exec();
}


returns:


In file included from ..\Qt\include/QtCore/qlist.h:1,
from ..\Qt\include/QtCore/../../src/corelib/kernel/qobject.h:50,
from ..\Qt\include/QtCore/qobject.h:1,
from ..\Qt\include/QtCore/../../src/corelib/io/qiodevice.h:46,
from ..\Qt\include/QtCore/qiodevice.h:1,
from ..\Qt\include/QtCore/../../src/corelib/xml/qxmlstream.h:45,
from ..\Qt\include/QtCore/qxmlstream.h:1,
from ..\Qt\include/QtCore/QtCore:3,
from ..\Qt\include\QtGui/QtGui:3,
from main.cpp:1:
..\Qt\include/QtGui/../../src/gui/graphicsview/qgraphicsitem.h: In member function 'void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QGraphicsLineItem]':
..\Qt\include/QtCore/../../src/corelib/tools/qlist.h:606: instantiated from 'void QList<T>::detach_helper() [with T = QGraphicsLineItem]'
..\Qt\include/QtCore/../../src/corelib/tools/qlist.h:114: instantiated from 'QList<T>::QList(const QList<T>&) [with T = QGraphicsLineItem]'
main.cpp:8: instantiated from here
..\Qt\include/QtGui/../../src/gui/graphicsview/qgraphicsitem.h:865: error: 'QGraphicsLineItem::QGraphicsLineItem(const QGraphicsLineItem&)' is private
..\Qt\include/QtCore/../../src/corelib/tools/qlist.h:370: error: within this context
..\Qt\include/QtGui/../../src/gui/graphicsview/qgraphicsitem.h:865: error: 'QGraphicsLineItem::QGraphicsLineItem(const QGraphicsLineItem&)' is private
..\Qt\include/QtCore/../../src/corelib/tools/qlist.h:383: error: within this context


Doing the same thing, but replacing QGraphicsLineItem with int or anything else works... I'm at a loss

squidge
21st January 2010, 18:16
Have you tried a list of references or pointers rather than a list of values?

rubenvb
21st January 2010, 19:11
QList<QGraphicsItem &>
fails because you can't new a reference

QList<QGraphicsItem *>
works, but I'm starting to think I should go about this differently.

Here's the thing:
I'm trying to make a QPlotAxis inherits QPlotItem inherits QGraphicsItem. The Axis containts the arrow (axis itself) and the labels (lines and text). I thought, well make a QList<QGraphicsLineItem> for the arrow, one for the markings (adaptive of window size) and a QList<QGraphicsTextItem> for the labels and axis name and work with that, but I guess it won't let me. What are the repercussions of working with a pointer list vs a normal one?

Or I could do it this way: create QPainterPath that does it all?

Thanks

aamer4yu
22nd January 2010, 04:47
QList<SomeClass> requiers SomeClass to have assignment operator (=) to make a copy of it.
Since QGraphicsItem doesnt seem to have it, you cannot use it directly in QList<QGraphicsItem> .
QList<QGraphicsItem*> is good, theres no problem with it.

boudie
22nd January 2010, 11:41
Your answer made me search the docs because I couldn't remember having read something like that, while your explanation does make sense...
Finally, I found a very interesting section:
http://qt.nokia.com/doc/4.6/containers.html#container-classes

Just posting for future reference...

rubenvb
22nd January 2010, 14:27
Ah, great link!


If you attempt to instantiate a QList<QWidget>, the compiler will complain that QWidget's copy constructor and assignment operators are disabled. If you want to store these kinds of objects in a container, store them as pointers, for example as QList<QWidget *>.

That says it all :)

Thanks, should have read the docs better, again...

ecanela
22nd January 2010, 19:01
The QGraphicsItem is a abstrac class and can't be used directly, QGraphicsItem::paint () is a pure virtual function, so you can't create a object of this class. QGraphics is used as a base class

because of this..



QList<QGraphicsItem> //don't compile. can't create a object of a abstract class
QList<QGraphicsItem *> // works because is posible create a pointer of a abstract class, polymorphism
QList<SubclassGraphicsItem> //works because the class is not abstract