PDA

View Full Version : setting up connections for dynamically created widgets



seux
11th August 2011, 14:51
Hi everyone,
I have a set of Pushbuttons which are created dynamically in a for() loop.
The code looks like this:


for(int i = 0; i < 32; i++)
{
btn = new MyButton(QString("%1").arg(i+1), i);
//Layout stuff which works...

connect(btn, SIGNAL(clicked()), this, SLOT(setLevel()));
}

The MyButton class just extends the PushButtons class. The first argument is the title, the second is an index variable (just an int). What i want to do is, that when one of the 32 buttons are pressed, the setLevel() functions is being called. I want to use the index to set the level. For example when the user presses button 21, the setLevel() function sets the level to 21.

It should be something like this, which is not working, i know...

connect(btn, SIGNAL(clicked()), this, SLOT(setLevel(btn.index)));

How can i do this?

mvuori
11th August 2011, 15:04
Slots need to have the same arguments as signals, so passing it as an argument doesn't work.

But QObjects have function sender() which returns a pointer to the sending object. Just cast it to your class and check which one it is.

seux
11th August 2011, 15:16
I thought more of writing an own signal that sends the index into the slot, but i do not know how. I'll take a look at the sender() function, maybe i can solve it with this.

Lesiok
11th August 2011, 17:16
Just read about QSignalMapper. This is solution for You.