PDA

View Full Version : How to disable sorting option in QMap



baluk
29th September 2010, 19:55
Hi,

I am using QMap function to keep key, value pairs. But the issue is by default QMap sort the elements in Ascending order which I don't want for my app. I would like to know if there is a way to stop sorting by defualt in QMap or is there any function that works as QMap but don't sort items. Any help would be great.

Thank You,
Baluk.

Lykurg
29th September 2010, 19:58
Its essential that QMap sorts its keys. If you would have read the documentation you would have seen the description of Qt's container classes...
QHash is what you are looking for.

SixDegrees
29th September 2010, 20:44
The question is: why don't you want the contents sorted? What problem is this causing? If you're trying to retain some other ordering that a sort interferes with, you may need to choose a different container class, or you may be able to override operator< to preserve the ordering you prefer. A hash is another option, but here the ordering is more difficult to preserve, although lookups are fast. Or a plain old array may be the best answer.

Maps, by their nature, order their contents. There's no getting around it.

baluk
30th September 2010, 09:08
Hi,

In my app I am writing some data to XMl file and display that data back in a Tbale widget. so While reading Xml data I am using QMap and insert the items into table, I want to show the items in the same order as they are in Xml file for adding and deleting purpose. But QMap Sorts the items bu Defulat. Now I have tried with QHash, but QHash retrieves the items in random order which is also giving me some problem.

Thank You,
Baluk

Lykurg
30th September 2010, 09:13
Then use QList. What's the problem?

baluk
30th September 2010, 09:52
Yes may be I can use QList, but what I am afraid of is if this approch makes confused. my QMap structre is QMap<QString, QMap<QString,QString> >, So in this case Qlist approach might be bit comlicated. isn't it so. This is just my basic opinion.

Thank You,
Baluk

Lykurg
30th September 2010, 09:57
Well it's hard to give a good advice in front, if in every post the conditions change;)
For keeping the order an easy approach is to store your items in a
QHash<QString, QMap<QString,QString> > and beside that you have a
QList<QString> where you store the order of your hash keys.

tbscope
30th September 2010, 10:04
my QMap structre is QMap<QString, QMap<QString,QString> >
That is a 3 dimensional structure, how do you show that in a table?

You can use your own container structure too of course.

QList<myContainerStructure>

baluk
30th September 2010, 10:46
I used the QMap iterator method to access the values and then insert them into the table .

Astronomy
30th September 2010, 11:44
Hi,
maybe you look at C++ STL Container classes. As Lykurg & others are saying, another container would maybe a better design- starting point. Since QMap is a assiciative container which does the sorting.. maybe you simply switch to another, better.., container?

take a look at:
The C++ standard library: a tutorial and handbook Von Nicolai M. Josuttis (i like this book:))
at Chapter: When to use which container.

http://books.google.com/books?id=n9VEG2Gp5pkC&printsec=frontcover&dq=josuttis+the+c%2B%2B+standard+template+lib&hl=de&ei=6GikTNq-NMHNswbh5aScCA&sa=X&oi=book_result&ct=result&resnum=1&ved=0CDEQ6AEwAA#v=onepage&q&f=false

at Table at page 227
If you found the right thing, maybe look at Qt classes, there are almost similar classes.
This Book also helps you to adapt your code.

Or Stroustrup, at page 521:
http://books.google.com/books?id=YR5CqZcFJU8C&printsec=frontcover&dq=stroustrup++the+c%2B%2B&hl=de&ei=xmqkTOW1OcKQswaslIW8CA&sa=X&oi=book_result&ct=result&resnum=1&ved=0CCgQ6AEwAA#v=onepage&q&f=false

there's an example: map<char*, int, Cstring_kleiner>& m;
which indicates a third parameter (it's a function..) Cstring_kleiner , maybe you can simplify your code?

greetz Astronomy

ChrisW67
1st October 2010, 03:57
Yes may be I can use QList, but what I am afraid of is if this approch makes confused. my QMap structre is QMap<QString, QMap<QString,QString> >, So in this case Qlist approach might be bit comlicated. isn't it so. This is just my basic opinion.


Which QMap is the one you want to stop ordering; the inner or the outer?
Perhaps these are worth trying?

QList<QPair<QString, QMap<QString,QString> > > // replaces the outer QMap
QMap<QString, QList<QPair<QString,QString> > > // the inner one replaced

Lykurg
1st October 2010, 05:56
I haven't thought through all aspects of http://marcmutz.wordpress.com/2010/07/29/sneak-preview-qlist-considered-harmful/, but a QList with QPair seems not to be the best choice.

baluk
1st October 2010, 10:10
I somehow have solved the issue by using QList as a index for inserting rows into table from the QMap. But need bit more lines of code. Thank you all for giving me good suggestions.