PDA

View Full Version : Connect with ArrayButton. Need an ID.



VireX
7th April 2007, 05:50
Ok,
My main class:

QList<QPushButton*> t_buttons;
t_buttons << new QPushButton("Host");
// Maybe can somehow use i in AddBox?
connect(t_buttons[i], SIGNAL(clicked()), this, SLOT(AddBox()));

void AddBox(){
// now how do I know which button was clicked???
}


I'm having trouble figuring out a way to connect them, emit won't work I dont think, and I don't know a way to override clicked(), or a way to give AddBox the correct ID, and I don't wanna really make like 50 functions lol.
Any Ideas?

marcel
7th April 2007, 08:15
Use QButtonGroup instead of QList. Like this:



QButtonGroup btnGroup;

btnGroup.addButton( new QPushButton( "Host" ), 0 );
connect( btnGroup, SIGNAL( clicked( int ) ), this, SLOT( AddBox( int ) ) );


Here I set an ID of 0 for the first button but you can use anything you want. The buttons are added with addButton.

When a button is clicked, it's id is passed to AddBox.

Marcel

VireX
7th April 2007, 17:57
You're aaaammaaazing...

VireX
7th April 2007, 19:16
Wait, there is still a problem.
t_buttons.addButton(new QPushButton("Box"), i);
connect(&t_buttons, SIGNAL(buttonClicked(int)), this, SLOT(AddBox(int)));

But now, whenever someone clicks a button.... It launches AddBox like 50 times. Because I have 50 buttons. BTW it's buttonClicked not clicked().

marcel
7th April 2007, 19:29
No, when someone clicks a button, let's say the btn with the id 5, then buttonClicked( 5 ) will be emitted and AddBox( 5 ) will be called.

Depends on the implementation of your AddBox function. It should be something like this:



void Class::AddBox( int btnIndex )
{
switch( btnIndex )
{
...
case 5:
// Do something...
break;
...
}
}


The slot will be called only once for each click.

Marcel.

VireX
7th April 2007, 19:41
....
t_buttons.addButton(new QPushButton("Box"), i);
connect(&t_buttons, SIGNAL(buttonClicked(int)), this, SLOT(AddBox(int)));
}
void CBox::AddBox(int i){
COBox = new COBox(i);
COBox->show();
}

Here's how I did it. But whenever I click any of the buttons, so let's say I click on button 34, and it will pop up about 55 (the total amount of buttons) new Windows of COBox!!! Like as if it's stuck in a loop somehow. The constructor of COBox changes the Windowtitle to the number i, and seems like they're all the correct one, but one CLICK on ANY button creates 55 new windows... can't figure out why.

marcel
7th April 2007, 19:54
Is connect(&t_buttons, SIGNAL(buttonClicked(int)), this, SLOT(AddBox(int))); in a for loop?

If it is ( most probably is ), then take it out of there! That is why you get so many slot calls for a single emit.

marcel
7th April 2007, 19:57
Put connect outside the for loop.
Connecting a signal to a slot 55 times has the effect you mentioned. The slot will get called 55 times the signal is emitted.

Regards,
Marcel.

VireX
7th April 2007, 20:15
Sorry, I'm just being very newb today... What a dumb mistake of me... thanks. (Forgot I was connecting the group rather than each button separately in a loop).

marcel
7th April 2007, 20:16
No problem. Could've happened to anyone...

aamer4yu
9th April 2007, 05:16
One more thing... if u have similar kind of functionality for the buttons, u can let the button handle that functionality instead of catching the event and then handling it.

For example I had a case where I wanted to change colors(by popping color dialog) for some data in list using buttons. Instead of using one slot and taking action based on which button was clicked, I put the color changing functionality in the button itself. Later I would retrieve the color from the button.
May be if u have similar situation u can use the technique....

But if u want different buttons perform different actions, above methods are fine...