有没有从QList<std::string>创建QList<QString>的简单方法
(不需要迭代QList<std::string>并将每个元素添加到QList<QString>)
发布于 2015-07-10 13:08:16
答案是否定的。你怎么可能在不迭代的情况下将一个转换成另一个?即使您使用的是某种类型的函数,它也会遍历列表。
发布于 2015-07-10 19:06:02
如果不遍历列表,就无法做到这一点。您仍然可以高效地执行此操作,从而避免不必要的副本和重新分配:
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));https://stackoverflow.com/questions/31331969
复制相似问题