View Full Version : Set QHash and QMap value directly

17th December 2009, 17:00
How to set QHash, QMultiHash, QMap and QMultiMap value directly ? For QList<int> I can do like this

QList<int>() << 1 << 2 << 3;

17th December 2009, 17:33
You can't because QHash etc are key-value-based.

17th December 2009, 20:21
You can't as QHash and QValue lack a method for inserting that returns a reference to the container. So you can't chain method calls.


template<typename KEY,typename VALUE>
struct MapConstructor : public QMap<KEY,VALUE>
MapConstructor<KEY,VALUE>& insert(const KEY &key, const VALUE &value)
QMap<KEY,VALUE>::insert(key, value);
return *this;

QMap<QString,int> aMap = MapConstructor<QString,int>().insert("hello",5).insert("world",5).insert("hell",4);

or you could take a look at Boost.Assign, so you don't have to reinvent the wheel.