[英]Convert QList<std::string> to QList<QString>
有沒有一種簡單的方法可以從QList<std::string>
創建QList<QString>
QList<std::string>
?
(無需迭代QList<std::string>
並將每個元素添加到QList<QString>
)
答案是不。 您怎么可能不進行迭代就將其轉換為另一種? 即使您使用某種功能,也要遍歷列表。
如果不遍歷列表,您將無法做到這一點。 您仍然可以有效地做到這一點,避免不必要的副本和重新分配:
QList<std::string> listStd;
listStd << "one" << "two" << "three";
QList<QString> listQt;
listQt.reserve(listStd.length());
for(const std::string& s : listStd)
{
listQt.append(QString::fromStdString(s));
}
// listQt: "one", "two", "three"
如果您不想進行轉換,則可以將std::string
直接保存為QString
,從而避免以后進行轉換。
QList<QString> lst; // or you can use the typedef QStringList
....
std::string s = getting_a_std_string_from_this_function();
lst.append(QString::fromStdString(s));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.