qmap

QMap iteration crash

匿名 (未验证) 提交于 2019-12-03 09:13:36
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using Qt 5.5 on Windows 8.1. When I run the code below, the application is able to get through one iteration, but crashes on the second one. 100% reproducible. (Copy/paste it into a Qt Creator instance and test; it might work for you). #include <QCoreApplication> #include <QDebug> #include <utility> using std::pair; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QMap<QString, pair<QString, QString> > table_def = { {"alpha", {"INTEGER", "PRIMARY KEY"}}, {"beta", {"VARCHAR", ""}}, {"gamma", {"VARCHAR", ""}}, {"delta",

How to deep copy QMap and other Qt containers

匿名 (未验证) 提交于 2019-12-03 08:42:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Generally speaking, what is the correct way to deep copy Qt containers? I'm not worried about deep copying the containers recursively, although addressing such would be helpful. 回答1: Despite what everyone will tell you - that you don't deep copy Qt containers - there are situations in which you simply need to perform an actual deep copy instead of just a shallow one. To do that, use detach() : container1 = container2; container1.detach(); 文章来源: How to deep copy QMap and other Qt containers

Iterating over a QMap with for

久未见 提交于 2019-12-03 04:52:45
问题 I've a QMap object and I am trying to write its content to a file. QMap<QString, QString> extensions; //.. for(auto e : extensions) { fout << e.first << "," << e.second << '\n'; } Why do I get: error: 'class QString' has no member named 'first' nor 'second' Is e not of type QPair ? 回答1: If you want the STL style with first and second , do this: for(auto e : extensions.toStdMap()) { fout << e.first << "," << e.second << '\n'; } If you want to use what Qt offers, do this: for(auto e :

Qt容器类之二:迭代器

匿名 (未验证) 提交于 2019-12-02 23:43:01
遍历一个容器可以使用迭代器(iterators)来完成,迭代器提供了一个统一的方法来访问容器中的项目。Qt的容器类提供了两种类型的迭代器:Java风格迭代器和STL风格迭代器。 如果只是想按顺序遍历一个容器中的项目,那么还可以使用Qt的foreach关键字。 Java风格的迭代器在Qt4中加入,比STL风格的迭代器更易于使用,但是以轻微的效率作为代价, 它们的API以Java的迭代器类为模型。 对于每个容器类,都有两种Java风格的迭代器类型:一种是只读,另一种是可读写。 容器 只读迭代器 可读写迭代器 QList<T>, QQueue<T> QListIterator<T> QMutableListIterator<T> QLinkedList<T> QLinkedListIterator<T> QMutableLinkedListIterator<T> QVector<T>, QStack<T> QVectorIterator<T> QMutableVectorIterator<T> QSet<T> QSetIterator<T> QMutableSetIterator<T> QMap<Key, T>, QMultiMap<Key, T> QMapIterator<Key, T> QMutableMapIterator<Key, T> QHash<Key, T>,

Assigning to nested QVariantMap

强颜欢笑 提交于 2019-11-30 12:53:00
#include <QtCore/QCoreApplication> #include <QVariant> #include <QtDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QVariantMap map; map["foo"] = QVariant(QVariantMap()); map["baz"] = "asdf"; qvariant_cast<QVariantMap>(map["foo"])["bar"] = "a"; qDebug() << qvariant_cast<QVariantMap>(map["foo"])["bar"].toString(); qDebug() << map["baz"].toString(); return a.exec(); } I am trying to assign to a QVariant within a nested QVariantMap. The first qDebug() outputs nothing, but the second outputs "asdf" as expected. How would I assign the "bar" key in the nested variable map to

Assigning to nested QVariantMap

谁都会走 提交于 2019-11-29 18:07:16
问题 #include <QtCore/QCoreApplication> #include <QVariant> #include <QtDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QVariantMap map; map["foo"] = QVariant(QVariantMap()); map["baz"] = "asdf"; qvariant_cast<QVariantMap>(map["foo"])["bar"] = "a"; qDebug() << qvariant_cast<QVariantMap>(map["foo"])["bar"].toString(); qDebug() << map["baz"].toString(); return a.exec(); } I am trying to assign to a QVariant within a nested QVariantMap. The first qDebug() outputs nothing,

Qt的容器类

孤人 提交于 2019-11-28 19:48:53
Qt提供了多种容器类,这些容器可以用于存储指定类型的数据项,并且可以实现对字符串列表的添加,存储,删除等操作。 容器类是基础模板的类,如常用的容器类 QList <T> ,T是一个具体的类型,可以是int,float等简单类型,也可以是QString,QDate等类,但不可以是QObject或任何其子类。T必须是一个可赋值的类型。 例如用 QList <T> 定义一个字符串列表的容器,其定义方法是: QList <QString> strList; 这样定义了一个QList容器类的变量strList,它的数据项是QString,所以strList可以用于处理字符串列表,例如: strList.append("one"); strList.append("two"); strList.append("three"); QString str = strList[0]; //str = "one" Qt的容器类大致分为 顺序容器 和 关联容器 。 顺序容器类 Qt的顺序容器类有QList,QLinkedList,QVector,QStack和QQueue。 QList QList是最常用的容器类,虽然它是以数组列表的形式实现的,但是在其前后添加数据非常快,QList以下标索引的方式对数据项进行访问。 QList用于添加,插入,替换,移动,删除数据项的函数有:insert()