How to disable sorting option in QMap
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.
Re: How to disable sorting option in QMap
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.
Re: How to disable sorting option in QMap
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.
Re: How to disable sorting option in QMap
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
Re: How to disable sorting option in QMap
Then use QList. What's the problem?
Re: How to disable sorting option in QMap
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
Re: How to disable sorting option in QMap
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
Code:
QHash<QString, QMap<QString,QString> >
and beside that you have a where you store the order of your hash keys.
Re: How to disable sorting option in QMap
Quote:
Originally Posted by
baluk
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.
Code:
QList<myContainerStructure>
Re: How to disable sorting option in QMap
I used the QMap iterator method to access the values and then insert them into the table .
Re: How to disable sorting option in QMap
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=n9V...page&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=YR5...page&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
Re: How to disable sorting option in QMap
Quote:
Originally Posted by
baluk
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?
Code:
QList<QPair<QString, QMap<QString,QString> > > // replaces the outer QMap
QMap<QString, QList<QPair<QString,QString> > > // the inner one replaced
Re: How to disable sorting option in QMap
I haven't thought through all aspects of http://marcmutz.wordpress.com/2010/0...dered-harmful/, but a QList with QPair seems not to be the best choice.
Re: How to disable sorting option in QMap
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.