Results 1 to 9 of 9

Thread: QMap problem

  1. #1
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QMap problem

    Hi, I am writing an application that needs to use QMapin the following way QMap<int, QMap<QString, QToolButton*> *> tabs. But now I want to insert value into the inner QMap<QString, QToolButton*>*, but the QMap::value()always return a constant that I cannot inset or modify any data in it.

    Do you guys know some solutions?
    Thanks in advance.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMap problem

    First, use operator[] instead of value().

    Apart from that: make sure you really need to store a pointer to a QMap.
    I doubt you need to do that. It is perfectly fine to have a map of maps.
    (And maybe use a typedef to get things more readable.)

    Qt Code:
    1. typedef QMap<QString,QToolButton>* TBMap;
    2. QMap<int,TBMap> tbmap;
    3. tbmap[1]["hello"] = new QToolButton(...);
    To copy to clipboard, switch view to plain text mode 

    HTH

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

    sophister (8th May 2009)

  4. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMap problem

    Quote Originally Posted by caduel View Post
    First, use operator[] instead of value().
    but value works fater then [].
    from docs
    In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a map. The reason is that operator[]() silently inserts an item into the map if no item exists with the same key (unless the map is const). For example, the following code snippet will create 1000 items in memory:
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMap problem

    I agree.
    But the OP wrote he wants to insert into the map. You can't do that with value().

  6. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMap problem

    but we can use insert, since QMap stores unique values.
    it's just IMHO.
    but I preffered this method.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMap problem

    but can you see the difference:
    Qt Code:
    1. T & operator[] ( const Key & key );
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. const T value ( const Key & key ) const;
    To copy to clipboard, switch view to plain text mode 
    ?
    operator[] is returning reference to the stored item, so you can change it with that reference, but value() is returning const copy of the item, so you can change it, and even if it won't be const it's still a copy so you would be changing the copy only.

    P.S. I copied finctions from docs.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. The following user says thank you to faldzip for this useful post:

    sophister (8th May 2009)

  9. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMap problem

    I know about difference.
    using value/insertValue you can achive the same result.
    Qt Code:
    1. ...
    2. QMap<int, QMap<QString,QToolButton> > tbmap;
    3. ...
    4. QMap<QString,QToolButton> map = tbmap.value(1);
    5. map.insertValue("toolButton", new QToolButton);
    6. tmap.insert(1, map);
    To copy to clipboard, switch view to plain text mode 
    the theread starter should remember that if
    map has a value and it is being reset then old value will be overwrote.
    Last edited by spirit; 8th May 2009 at 06:32.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMap problem

    this is test of these approaches
    Qt Code:
    1. #include <QMap>
    2. #include <QTime>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. Q_UNUSED(argc);
    8. Q_UNUSED(argv);
    9.  
    10. QTime startInsertionsTest1;
    11. startInsertionsTest1.start();
    12. qDebug() << "start insertion (insert/value): " << startInsertionsTest1;
    13. QMap<int, QMap<QString, QString> > map1;
    14. for (int i = 0; i < 1000; ++i) {
    15. QMap<QString, QString> tempMap;
    16. for (int j = 0; j < 1000; ++j)
    17. tempMap.insert(QString::number(j), QString::number(j));
    18. map1.insert(i, tempMap);
    19. }
    20. const int test1 = startInsertionsTest1.elapsed();
    21. qDebug() << "end: " << test1;
    22.  
    23. QTime startInsertionsTest2;
    24. startInsertionsTest2.start();
    25. qDebug() << "start insertion (operator []): " << startInsertionsTest2;
    26. QMap<int, QMap<QString, QString> > map2;
    27. for (int i = 0; i < 1000; ++i) {
    28. QMap<QString, QString> tempMap;
    29. for (int j = 0; j < 1000; ++j)
    30. tempMap[QString::number(j)] = QString::number(j);
    31. map2[i] = tempMap;
    32. }
    33. const int test2 = startInsertionsTest2.elapsed();
    34. qDebug() << "end: " << test2;
    35.  
    36. QTime startInsertionsTest3;
    37. startInsertionsTest3.start();
    38. qDebug() << "start updatetion (insert/value): " << startInsertionsTest3;
    39. for (int i = 0; i < 1000; ++i) {
    40. QMap<QString, QString> tempMap(map1.value(i));
    41. QMap<QString, QString>::iterator it(tempMap.begin());
    42. for (; it != tempMap.end(); ++it)
    43. it.value() = "a";
    44. map1.insert(i, tempMap);
    45. }
    46. const int test3 = startInsertionsTest3.elapsed();
    47. qDebug() << "end: " << test3;
    48.  
    49. QTime startInsertionsTest4;
    50. startInsertionsTest4.start();
    51. qDebug() << "start updatetion (operator []): " << startInsertionsTest4;
    52. for (int i = 0; i < 1000; ++i) {
    53. QMap<QString, QString> tempMap(map2[i]);
    54. QMap<QString, QString>::iterator it(tempMap.begin());
    55. for (; it != tempMap.end(); ++it)
    56. it.value() = "a";
    57. map1[i] = tempMap;
    58. }
    59. const int test4 = startInsertionsTest4.elapsed();
    60. qDebug() << "end: " << test4;
    61.  
    62. qDebug() << "--------------------";
    63. qDebug() << "insertion using (insert/value) time: " << test1;
    64. qDebug() << "insertion using (operator[]) time: " << test2;
    65. qDebug() << "insertion diff (insert/value - operator[]): " << test1 - test2;
    66.  
    67. qDebug() << "updation using (insert/value) time: " << test3;
    68. qDebug() << "updation using (operator[]) time: " << test4;
    69. qDebug() << "updation diff (insert/value - operator[]): " << test3 - test4;
    70. return;
    71. }
    To copy to clipboard, switch view to plain text mode 
    this is a result
    start insertion (insert/value): QTime("10:03:22")
    end: 11281
    start insertion (operator []): QTime("10:03:33")
    end: 11578
    start updatetion (insert/value): QTime("10:03:45")
    end: 4047
    start updatetion (operator []): QTime("10:03:49")
    end: 6703
    --------------------
    insertion using (insert/value) time: 11281
    insertion using (operator[]) time: 11578
    insertion diff (insert/value - operator[]): -297
    updation using (insert/value) time: 4047
    updation using (operator[]) time: 6703
    updation diff (insert/value - operator[]): -2656
    PS. maybe it's not so good, but anyway...
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. The following user says thank you to spirit for this useful post:

    sophister (8th May 2009)

  12. #9
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap problem

    Thank you all very much!!!

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  3. Qmap problem on a static lib
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2007, 12:11
  4. QMap Problem with arguments.
    By ankurjain in forum Qt Programming
    Replies: 1
    Last Post: 24th May 2006, 12:12
  5. QMap destructor bug
    By Ruud in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2006, 09:08

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.