PDA

View Full Version : Set QHash and QMap value directly



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



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

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

caduel
17th December 2009, 19: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.

try

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.