PDA

View Full Version : Problem using QPointer



weaver4
17th February 2010, 20:54
When I use this statement:

QPointer<QStringList> sl = new QStringList();

I get this error:

cannot convert 'QStringList' to 'QObject' in initialization

But when I try replacing QStringList with QLabel it seems to work OK.

Lykurg
17th February 2010, 21:13
QPointer can only be used with classes that are a subclass of QObject. QStringList is not!

And by the way: It's a bad idea to create a string list on the heap. There is no need since the string list is implicitly shared.

weaver4
17th February 2010, 22:18
What do you mean by "implicitly shared"? I don't understand why it is a bad idea to create it on the heap?

Lykurg
17th February 2010, 22:56
What do you mean by "implicitly shared"?
See http://doc.trolltech.com/4.6/implicit-sharing.html

I don't understand why it is a bad idea to create it on the heap?
An int or double you normaly create on the stack, not the heap. So trat QStringList like a basic type. "bad" because it is faster to create it on the stack and and you don't have to take care about deleting etc.

weaver4
18th February 2010, 13:29
I am kinda new to QT, I did not know about the "implicitly shared" objects; pretty cool.

I have made a subclass of QStringList that adds the functions: LoadFromFile and SaveToFile. Since this is a subclass would it be "implicitly shared" too?

Thanks for your help.

Lykurg
18th February 2010, 14:23
I have made a subclass of QStringList that adds the functions: LoadFromFile and SaveToFile. Subclassing QStringList is pretty "dangerous". If you only need that two methods (that don't chance anything at the string list) it is better to define (global) functions to do that.

weaver4
18th February 2010, 15:18
Why is it "Dangerous"? (I said I was new to QT, trying to figure this Framework out.)

Lykurg
18th February 2010, 16:09
You won't blew your computer up:), but things can easily go wrong (performence loose, leaks) if you are not 100% sure of what you are doing. This is the case for such basic Qt storing containers. And would you subclass int or double to ad a save function? Treat QStringList, QString, QPixmap etc. as such generic typs.
And to your question if your subclass looses implicit sharing: I don't know for sure. It seems that it don't. But I am not sooo deep into the Qt sources that I can say it for sure.

Coises
20th February 2010, 05:05
I have made a subclass of QStringList that adds the functions: LoadFromFile and SaveToFile. Since this is a subclass would it be "implicitly shared" too?

Remember that “implicit data sharing” is nothing magical: it just means the class contains a handle to reference-counted data that’s shared as long as the class is certain that the data won’t change. Typical implementations of std::string work the same way.

We commonly speak of “implicitly shared” classes, but more precisely, it’s data that is implicitly shared. In the case of QStringList, the QString handles and organizational framework that make up the QList of QStrings are shared. Deriving from the QStringList won’t change that; in fact, the data in QStringList is implicitly shared because it is derived from QList<QString>, and QLists are implicitly shared.

Of course, that won’t make any additional data your class contains or manages implicitly shared.