[Qt-interest] QList and QUrl problem

Denis Akhmetzyanov dakhmetzyanov at smartlabs.tv
Fri Mar 26 13:18:23 CET 2010


Hi,
QUrl class is implicit sharing and QUrl::setUrl() method internally doesn't
call to detach() (review src of Qt 4.6.2). Some methods do that, for
example:
setScheme(), setAuthority(), setUserInfo(), setUserName(), setPassword(),
setHost(), setPort(), setPath(), setEncodedQuery(). I.e. this code works
fine:
    QList<QUrl> test;
    QUrl url;
    url.setPath("1");
    test.append(url);
    qDebug()<<test[0];
    url.setPath("2");
    test.append(url);
    qDebug()<<test[0];
    url.setPath("3");
    test.append(url);
    qDebug()<<test[0] << test[1] << test[2];

Output:

 QUrl( "1" )

 QUrl( "1" )
 QUrl( "1" )   QUrl( "2" )   QUrl( "3" )

Causes of this behaviour of the QUrl::setUrl() are unknown for me (may be
"value object" pattern). So for add different urls you could use:
    QList<QUrl> test;
    test.append(QUrl("1"));
    test.append(QUrl("2"));
    test.append(QUrl("3"));
    qDebug()<<test[0] << test[1] << test[2];

Output is:

 QUrl( "1" )   QUrl( "2" )   QUrl( "3" )



2010/3/26 ice stone <icestone6124 at yahoo.com>

> I am trying to figure out a problem involving QList<QUrl>. Here is my code:
>
>  QList<QUrl> test;
>  QUrl url;
>  url.setUrl("1");
>  test.append(url);
>  qDebug()<<test[0];
>  url.setUrl("2");
>  test.append(url);
>  qDebug()<<test[0];
>  url.setUrl("3");
>  test.append(url);
>  qDebug()<<test[0] << test[1] << test[2];
>
> And the output is:
>  QUrl( "1" )
>  QUrl( "2" )
>  QUrl( "3" )   QUrl( "3" )   QUrl( "3" )
>
> My understanding of QList is that it allocates a memory in heap for every
> inserted item and delete them when they are removed from the list. But the
> output here seems to point to a single QUrl object for all three items.
> Anyone can help me to solve this puzzle? Thanks a lot.
>
> Yan
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100326/7cc3d718/attachment.html 


More information about the Qt-interest-old mailing list