[英]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.