Results 1 to 8 of 8

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

  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.

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

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

    The problem is that I intended to have QButtons as well as TextEdits in a common order.. and I can't store them together in a container. So I got the idea to use two containers with an index to order both - buttons as well as textedits in the Dialog.

    But I can't "recall" the pointers in the QMap to add them to the window's Layoutmanager.
    Qt Code:
    1. horizontalLayout->addWidget (mapOfButtons->value (int index = 10) );
    To copy to clipboard, switch view to plain text mode 
    It doesn't work.

    Thank you.

    I am sorry
    That's it!
    Thank you very much!

  6. #6
    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*> ?

    The problem is that I intended to have QButtons as well as TextEdits in a common order..
    If you mean by that that both buttons and TextEdits have the same order (so that a certain button corresponds with a certain text edit) that is not a problem but an advantage!.
    Then use to lists, one for buttons, one for edits, and the the same index will apply correctly for both.

    It doesn't work.
    What do you mean by 'work' - compile?
    It should not compile since the syntax is wrong:
    Qt Code:
    1. horizontalLayout->addWidget (mapOfButtons->value (10) );
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    QMap overrides the [] method, so you could just pass the key as the parameter rather than using the explicit method.

    But from what you've said, I would have thought QVector would be a better container. It's random access and you can insert and remove items from anywhere in the array. You would lose the 'key' part, but you can access them via the position in the array (0 - n-1 as per usual)

  8. #8
    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*> ?

    It's random access and you can insert and remove items from anywhere in the array.
    I thought he wanted a strict order... if its random order he needs, then yes, QVector would be better.
    ==========================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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.