2012-11-25 12 views
3

我的數據庫中的數據在QVariantList中,我想通過它循環並獲取名字。從QVariantList中獲取字符串

QVariantList sqlData = database->loadDatabase("quotes.db", "quotes"); 
for (int i = 1; i <= sqlData.size(); i++) 
    { 
     qDebug() << sqlData.value(i); 
    } 

這將產生:

Debug: QVariant(QVariantMap, QMap(("firstname", QVariant(QString, "Glenford")) ("id" , QVariant(qlonglong, 2)) ("lastname" , QVariant(QString, "Myers")) ("quote" , QVariant(QString, "We try to solve the problem by rushing through the design process so that enough time will be left at the end of the project to uncover errors that were made because we rushed through the design process."))) ) 

如何我剛剛調試 「名字」 的價值?例如debug = Glenford。

感謝

+0

嘗試http://qt-project.org/doc/qt-4.8/qvariant.html#value – 2012-11-25 20:50:59

+0

那不幫助,我怎麼能目標先看名字? – panthro

+4

看着Qt Docs,我認爲'sqlData.value(i).toMap()。value(「firstname」);'可能適合你。 – 2012-11-25 21:08:50

回答

4

的QVariant列表QVariants列表,對象都是有特定的迭代器。

QVariantList sqlData = database->loadDatabase("quotes.db", "quotes"); 
for (QVariantList::iterator j = sqlData.begin(); j != sqlData.end(); j++) 
{ 
    qDebug() << "iterating through QVariantList "; 
    qDebug() << (*j).toString(); // Print QVariant 
}