欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT QStringList 与 QString 相互转换

程序员文章站 2024-03-17 18:58:28
...

QStringList 转 QString

QStringList listemp;
listemp << "Apple" << "Grape" << "Orange" << "Banana";
QString str = listemp.join(",");
qDebug() << "\r\nstr:" << str;
//输出结果如下
//str: "Apple,Grape,Orange,Banana"

QString转 QStringList

QString strtmp = "apple:5.99;grape:12.5;orange:6.99";
QStringList fruits = strtmp.split(";");
qDebug() << "\r\nfruits:" << fruits;
//fruits: ("apple:5.99", "grape:12.5", "orange:6.99")

//
for(int i=0; i < fruits.size(); i++)
{
    QStringList fruit = fruits[i].split((":"));
    qDebug() << "fruit:" << fruit;
    /*
    fruit: ("apple", "5.99")
    fruit: ("grape", "12.5")
    fruit: ("orange", "6.99")
    */
}