PDA

View Full Version : How do I read a QMap<int, QPushButton*> ?



crowBar
30th December 2010, 13:21
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 :)

Zlatomir
30th December 2010, 13:53
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 (http://doc.qt.nokia.com/4.7/qmap.html#value) to doc

crowBar
30th December 2010, 22:07
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.

high_flyer
30th December 2010, 22:12
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:


//QPushButton button ("Hello"); //button is allocated on the stack, and it gets destroyed at the end of the scope, probably a method.
//It should be:
mapButtons->insert(10, new QPushButton("Hello",this));

crowBar
30th December 2010, 22:30
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.

horizontalLayout->addWidget (mapOfButtons->value (int index = 10) );
It doesn't work.

Thank you.

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

high_flyer
30th December 2010, 22:33
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:

horizontalLayout->addWidget (mapOfButtons->value (10) );

squidge
30th December 2010, 22:37
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)

high_flyer
30th December 2010, 22:39
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.