Results 1 to 7 of 7

Thread: QStringlist To QString

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QStringlist To QString

    HI All
    CAn any body tell me how to change QStringlist object to QString type.
    my code is QStringList list1 = str.split(".");
    i need to change list1 to Qstring type.
    I tried like this reinterpret_cast<QString>(list1). but giving error.
    THank you all.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QStringlist To QString

    I am not sure I understand what you really intent to do, but If you want to change a QStringList into a QString, I guess join() is the operation you are looking for:
    QString QStringList::join ( const QString & separator ) const
    a QStringList is fundamantally different from a QString. It is actually a QList<QString> with some added convenience functions like split and join. So casting doesn't really makes sense. You can implement a cast operation for it on your own if you derive from QStringList and add a cast operator returning something like the joined string but I don't see what purpose that would be good for.

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. class StringList : public QStringList
    5. {
    6. public:
    7. operator QString() const { return this->join(" "); }
    8. };
    9.  
    10. int main(int argc, char* argv[])
    11. {
    12. QApplication(argc, argv);
    13. StringList sl;
    14. sl << "one" << "two" << "three";
    15. qDebug() << (QString)sl;
    16.  
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by momesana; 27th October 2007 at 11:12.

  3. The following user says thank you to momesana for this useful post:

    phillip_Qt (30th October 2007)

  4. #3
    Join Date
    Sep 2006
    Posts
    6
    Thanks
    1
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QStringlist To QString

    A QStringList strlist is a list of QString .... so strlist[0] is a QString.

  5. The following 2 users say thank you to almost for this useful post:

    Gokulnathvc (19th March 2011), phillip_Qt (30th October 2007)

  6. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    69
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStringlist To QString

    Hi,

    Here you know that your QStringList will be only a single QString when you split it because you know that only there is one QString, I'm right?

    So,

    Qt Code:
    1. QStringList list1 = str.split(".");
    2. QString myString = list1.at(0); //You know that only is there one QString in the list
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  7. The following user says thank you to ^NyAw^ for this useful post:

    phillip_Qt (30th October 2007)

  8. #5
    Join Date
    May 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStringlist To QString

    Quote Originally Posted by phillip_Qt View Post
    HI All
    CAn any body tell me how to change QStringlist object to QString type.
    my code is QStringList list1 = str.split(".");
    i need to change list1 to Qstring type.
    I tried like this reinterpret_cast<QString>(list1). but giving error.
    THank you all.

    Here is the solution :


    Qt Code:
    1. QStringList strList;
    2. strList << "bird" << "tree" << "water" ;
    3.  
    4. QString str = strList.join(""); // str = "birdtreewater";
    5. str = strList.join(","); // str = "bird,tree,water";
    To copy to clipboard, switch view to plain text mode 
    Last edited by CuteKid; 22nd May 2014 at 23:36.

  9. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,935
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QStringlist To QString

    Nice. I am sure that after 6 1/2 years of waiting since 2007, phillip_Qt was overjoyed to get your answer.

  10. #7
    Join Date
    Jun 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: QStringlist To QString

    Quote Originally Posted by CuteKid View Post
    Here is the solution :


    Qt Code:
    1. QStringList strList;
    2. strList << "bird" << "tree" << "water" ;
    3.  
    4. QString str = strList.join(""); // str = "birdtreewater";
    5. str = strList.join(","); // str = "bird,tree,water";
    To copy to clipboard, switch view to plain text mode 
    Thanks phillip_Qt

Similar Threads

  1. QStringList
    By dragon in forum Newbie
    Replies: 2
    Last Post: 8th June 2007, 17:26
  2. QString static callback function from CURL
    By tpf80 in forum Qt Programming
    Replies: 12
    Last Post: 16th May 2007, 20:47
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  4. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10

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.