PDA

View Full Version : QStringlist To QString



phillip_Qt
27th October 2007, 07:28
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.

momesana
27th October 2007, 10:55
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.



#include <QApplication>
#include <QtGui>

class StringList : public QStringList
{
public:
operator QString() const { return this->join(" "); }
};

int main(int argc, char* argv[])
{
QApplication(argc, argv);
StringList sl;
sl << "one" << "two" << "three";
qDebug() << (QString)sl;

return 0;
}

almost
29th October 2007, 08:14
A QStringList strlist is a list of QString .... so strlist[0] is a QString.

^NyAw^
29th October 2007, 09:55
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,



QStringList list1 = str.split(".");
QString myString = list1.at(0); //You know that only is there one QString in the list

CuteKid
22nd May 2014, 23:22
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 :



QStringList strList;
strList << "bird" << "tree" << "water" ;

QString str = strList.join(""); // str = "birdtreewater";
str = strList.join(","); // str = "bird,tree,water";

d_stranz
23rd May 2014, 00:14
Nice. I am sure that after 6 1/2 years of waiting since 2007, phillip_Qt was overjoyed to get your answer. ;)

emtubador
19th June 2015, 14:17
Here is the solution :



QStringList strList;
strList << "bird" << "tree" << "water" ;

QString str = strList.join(""); // str = "birdtreewater";
str = strList.join(","); // str = "bird,tree,water";

Thanks phillip_Qt