Results 1 to 6 of 6

Thread: QMap

  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

    Hi, I want to modify the value in a QMap, but when I tried to modify it through QMap::value(), I failed, because this method always return a value which is const(cannot be changed).
    Do you guys know how to change the value in QMap??
    Thanks!

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap

    You can modify the value using [] operator. Some like this

    Qt Code:
    1. map["one"] = 1;
    2. map["three"] = 3;
    3. map["seven"] = 7;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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

    Thanks, but if I am using QMap in this way, QMap<int, QMap<QString, QToolButton *> >, then how can I add QToolButton into the inner QMap??

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap

    Like this

    Qt Code:
    1. map[1]["one"] = toolButton
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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

    I use this, but the application crashed:
    Qt Code:
    1. QMap<QString, QToolButton *> *currentButtons = const_cast<QMap<QString, QToolButton *>*>(tabs.value(ui->tabWidget->currentIndex()));
    2. currentButtons->insert(path, btn); //This line is where the error lie
    To copy to clipboard, switch view to plain text mode 

    I am really puzzled.
    Did I fail to const_cast the const into non-cosnt so that I cannot insert a value into a const variable??

  6. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMap

    There is an iterator for map.
    I'm a rebel in the S.D.G.

Similar Threads

  1. QMap as function argument...
    By cydside in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 17:59
  2. How to implement operator<() for QMap
    By learning_qt in forum Qt Programming
    Replies: 6
    Last Post: 9th January 2009, 10:21
  3. QMap model data
    By gyre in forum Newbie
    Replies: 3
    Last Post: 9th December 2007, 22:19
  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.