[Kstars-devel] Porting to Qt4: avoid pointers in QLists?
Jason Harris
kstars at 30doradus.org
Sun Sep 11 02:46:29 CEST 2005
Hello,
I am starting to work on porting the code to Qt4. It's going to be a pretty
big job, because we make very heavy use of classes that no longer exist.
Before I really start ripping things apart, I thought it would be useful to
discuss some potential guidelines.
For this thread, let's concentrate on the QPtrList -> QList move. This is
going to be the biggest chore; we use QPtrList just about everywhere.
From the Qt4 porting guide
(http://doc.trolltech.com/4.0/porting4.html#qptrlist-t), we learn that the
equivalent of this old code:
QPtrList<SkyObject> objList;
//assume the list gets populated somehow...
for ( SkyObject *o = objList.first(); o; o = objList.next() ) {
//do stuff to o
}
is now:
QList<SkyObject*> objList;
//assume the list gets populated somehow...
for ( int i=0; i < objList.size(); ++i ) {
SkyObject *o = objList[i];
//do stuff to o
}
(note: there is also a QLinkedList class, if an iterator-type accessor is
somehow preferred to the [] random-access operator...)
However, there is no auto-delete for QList items. So, we would need to do
this at some point to avoid memory leaks:
while ( ! objList.isEmpty() ) delete objList.takeFirst();
Another option I want to consider is to simply avoid pointers as QList
members. Then we don't need to worry about deleting list members. I don't
think performance will be impacted; QList still uses pointers internally, and
the trolls claim that QList access is very fast. In fact, the docs for QList
claim that QList is usually faster than QVector, even though QVector
guarantees that the items are stored at adjecent locations in memory.
Does anyone have objections to adopting a guideline that QLists should not
have pointer members? Is there a downside that I am missing?
thanks,
Jason
--
KStars: KDE Planetarium
http://edu.kde.org/kstars
More information about the Kstars-devel
mailing list