Results 1 to 8 of 8

Thread: Problem With QMap

  1. #1
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Problem With QMap

    Hi guys.
    I have ordering problem with QMap. I've googled it but my problem didn't solved.
    Suppose I have QMap<QString,int>. After i insert some values into QMap the ordering is not kept. I didn't find any substitution option.
    Is there any (key, value) pairs container class int Qt that keep the ordering of keys.

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem With QMap

    I've solved it by this way : QList<QPair<T1,T2> > and that’s ok

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem With QMap

    Can you give an example of keys you insert and order you expect to get?

    QMap should be sorted by keys, more precisely sorted by operator<() of the key type.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem With QMap

    I want to key be sorted as insertion

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem With QMap

    thats what happens in qmap.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    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: Problem With QMap

    A QMap is not a list, it is a collection of mappings between a value of one type and a value of another. The keys of the QMap are kept in sorted order by value, which has nothing to do with the order of insertion. A QMap can have only one key of a given value and subsequent insertions with the same key will replace the existing map entry (contrast that with QMultiMap).

    Your solution with QList and QPair is one option. Another would be a QList of structure, which allows you to chose meaningful field names rather than use QPair's "first" and "second").
    Qt Code:
    1. struct MyData {
    2. QString stringValue;
    3. int someIntegerValue;
    4. };
    5. QList<MyData> list;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem With QMap

    Yeah I know thanks for all
    I've used : QList< QPair<T1,T2> > . Is this right way?

  8. #8
    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: Problem With QMap

    Yes, it is a way.

  9. The following user says thank you to ChrisW67 for this useful post:

    alizadeh91 (12th March 2013)

Similar Threads

  1. QMap problem
    By sophister in forum Qt Programming
    Replies: 8
    Last Post: 8th May 2009, 09:27
  2. QMap and static problem
    By longtrue in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 00:14
  3. Problem with QMap
    By comlink21 in forum Qt Programming
    Replies: 1
    Last Post: 10th December 2007, 08:33
  4. Qmap problem on a static lib
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2007, 13:11
  5. QMap Problem with arguments.
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 13:12

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.