Results 1 to 4 of 4

Thread: Casting QStringList to QList<QVariant>

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Casting QStringList to QList<QVariant>

    Can I simply cast this using a static/dynamic/const cast? If so, what is the syntax for it?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Casting QStringList to QList<QVariant>

    QStringList is a QList<QString>. But it is not possible cast it to QList<QVariant>. That's two template containers containing different types.

    Edit: BTW, see qCopy. Maybe it suits for your need:
    Qt Code:
    1. QStringList stringList;
    2. stringList << "one" << "two" << "three";
    3.  
    4. QVector<QVariant> vector(strings.size());
    5. qCopy(stringList.begin(), stringList.end(), vector.begin()); // works because QVariant has
    6. // a constructor taking QString
    7.  
    8. QList<QVariant> variantList = vector.toList(); // if you really need a list
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 5th October 2006 at 21:30. Reason: wrapped too long line
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Casting QStringList to QList<QVariant>

    The only thing you can do is:

    Qt Code:
    1. QList<QVariant> variantlist;
    2. foreach(QString s, slist){
    3. variantlist << s;
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Casting QStringList to QList<QVariant>

    Thanks. You two are most helpfull Don't you ever get tired of teh n00bs?

Similar Threads

  1. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01
  2. QStringList with Signal/Slot
    By Sivert in forum Qt Programming
    Replies: 4
    Last Post: 3rd May 2006, 20:34
  3. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48
  4. qt4 QStringList with model/view
    By incapacitant in forum Newbie
    Replies: 23
    Last Post: 16th March 2006, 19:39
  5. need help to classify some QStringList
    By patcito in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 21:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.