Results 1 to 8 of 8

Thread: How do I read a QMap<int, QPushButton*> ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How do I read a QMap<int, QPushButton*> ?

    Hey,

    I created a project containing a QMap with QPushButtons in it:

    QMap<int, QPushButton*> *mapButtons;
    mapButtons = new QMap<int, QPushButton*> ();

    now I created a button and inserted it:

    QPushButton button ("Hello");
    mapButtons->insert(10, &button);

    But do I get the Button out of the map again?
    I tried:

    QPushButton *button1 = mapButtons->value(10);

    I had lots of attempt that failed all completely.
    I look forward to your comment

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How do I read a QMap<int, QPushButton*> ?

    You need to use the value ( const Key & key ) const member function of the QMap.

    But why do you need button pointers in QMap?

    LE: or another solution is to use value ( const Key & key, const T & defaultValue ) const because you have pointers and you might want to return NULL pointer in case there is no key
    link to doc

  3. #3
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do I read a QMap<int, QPushButton*> ?

    well, my intentions were to enable the user to add or delete per mouse click QPushButton s to the Layout of a QDialog. I got no idea but to use some kind of container to store the buttons.

    As I considered the order to be important I thought of using a QMap that enables me to store each Button with an integer.

    I always tried to use the QMap's value(..) function, it didn't work nonetheless.

    Thank you for your ideas.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How do I read a QMap<int, QPushButton*> ?

    As I considered the order to be important I thought of using a QMap that enables me to store each Button with an integer.
    If the order of the buttons is the same as the order in which they are added to the container, then QList would probably be better and simpler to use.

    EDIT:
    in your original post your code to extract the values from the list is correct.
    But you are doing someting else wrong:
    Qt Code:
    1. //QPushButton button ("Hello"); //button is allocated on the stack, and it gets destroyed at the end of the scope, probably a method.
    2. //It should be:
    3. mapButtons->insert(10, new QPushButton("Hello",this));
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 30th December 2010 at 22:18.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2010, 09:30
  2. QMap
    By sophister in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2009, 10:05
  3. Is QMap efficient in case of frequent read access ?
    By yellowmat in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2006, 08:20
  4. Reg - QMap(Qt3.3.4)
    By suresh in forum Newbie
    Replies: 3
    Last Post: 18th October 2006, 22:04
  5. Replies: 3
    Last Post: 26th September 2006, 12:16

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
  •  
Qt is a trademark of The Qt Company.