Results 1 to 13 of 13

Thread: How to disable sorting option in QMap

  1. #1
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.

  3. The following user says thank you to Lykurg for this useful post:

    baluk (29th September 2010)

  4. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  5. #4
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to disable sorting option in QMap

    Then use QList. What's the problem?

  7. #6
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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
    Qt Code:
    1. QHash<QString, QMap<QString,QString> >
    To copy to clipboard, switch view to plain text mode 
    and beside that you have a
    Qt Code:
    1. QList<QString>
    To copy to clipboard, switch view to plain text mode 
    where you store the order of your hash keys.

  9. #8
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to disable sorting option in QMap

    Quote Originally Posted by baluk View Post
    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.
    Qt Code:
    1. QList<myContainerStructure>
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default 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 .

  11. #10
    Join Date
    May 2009
    Location
    Vienna
    Posts
    91
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Last edited by Astronomy; 30th September 2010 at 11:53.

  12. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to disable sorting option in QMap

    Quote Originally Posted by baluk View Post
    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?
    Qt Code:
    1. QList<QPair<QString, QMap<QString,QString> > > // replaces the outer QMap
    2. QMap<QString, QList<QPair<QString,QString> > > // the inner one replaced
    To copy to clipboard, switch view to plain text mode 

  13. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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.

  14. #13
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

Similar Threads

  1. Disable Move option in window
    By BalaQT in forum Qt Programming
    Replies: 10
    Last Post: 27th December 2009, 06:37
  2. Is QTimer is the only option ?
    By nrabara in forum Newbie
    Replies: 6
    Last Post: 6th May 2009, 06:59
  3. using the lib template dll option on *nix
    By jamadagni in forum Qt Programming
    Replies: 3
    Last Post: 19th April 2007, 13:14
  4. rtti option
    By sar_van81 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 5th January 2007, 15:10
  5. How can I disable the QListView Sorting?
    By darpan in forum Qt Programming
    Replies: 3
    Last Post: 27th June 2006, 10:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.