PDA

View Full Version : combine QLisdt and a simple structure



qt_gotcha
24th January 2011, 10:50
Most posts on this subject go way beyond what I want, and I quickly loose track.

I have a structure

typedef structure LDD_POINT{
int row;
int col;
}LDD_POINT;

in my class I define:


QList <LDD_POINT> *lddlist;

then with each new point in the code:


ldd_point *p = new LDD_POINT;
lddlist->append(p);

which doesn't work:
c:\Qt\2010.05\qt\include\QtCore\..\..\src\corelib\ tools\qlist.h:493: candidates are: void QList<T>::append(const T&) [with T = LDD_POINT]
c:\Qt\2010.05\qt\include\QtCore\..\..\src\corelib\ tools\qlist.h:819: void QList<T>::append(const QList<T>&) [with T = LDD_POINT]

So obviously I am doing something too simple. I don't understand the const T stuff, what is the correct format, or point me to an example...

Thanks

Zlatomir
24th January 2011, 11:11
That is because your list contains objects (you declare it: QList <LDD_POINT>) and you are trying to add pointers to LDDD_POINT (append(p) )

One solution is to use QList<YourStruct*>

And don't forget to iterate over the QList and delete all the pointers, that should be done after you are done working with that pointers and before the QList gets out of scope.

qt_gotcha
24th January 2011, 14:10
thanks, I also found in an example
QList <LDD_POINT const *> listldd;
but I do know the effect of adding the const in this case

Zlatomir
24th January 2011, 17:16
Short answer is that QList<TYPE const*> will contain pointers to constant TYPE objects... so that you can't modify the objects using the pointer in the QList. (but you can modify pointers to "point" elsewhere)


A longer answer can be found here (http://www.parashift.com/c++-faq-lite/const-correctness.html) (i really recommend you read it)

wysota
25th January 2011, 01:47
And I suggest you don't use pointers.

QList<LDD_POINT> lddlist; // no pointers
lddlist.append(LDD_POINT()); // no pointers, no "new" operator