tags:

views:

40

answers:

3

Hi.,

in my project i added Custom Widget inherit from QWidget class. the shape is Circle. i added this widget in to the parent Widget.

if the custom button clicked then how to find which button clicked?

Adding custom button to Parent Widget:

void ShotViewCTRL::addShot(QString shotNanme)
{
    ShotButton *btnShot=new ShotButton(this);
    btnShot->shotName=shotNanme;

    connect(btnShot,SIGNAL(Shot_Selected()),this,SLOT(SHOT_CLICKED()));
    btnShot->CreateButton();
    btnShot->show();

}

my parent widget is "ShotViewCTRL"(inherit from QWidget). child widget is "ShotButton" (custom control, inherit from Qwidget)

Control working fine. it's sending sending to parent object. in my problem i added same custom control 10 times.

i need to find which control is clicked? Please help me to find the solution.

i refered the Qt documentation to find the Child Widget, but i did't understand. please any one having some code sample that's great.

A: 

you specify different slots for different buttons with same signal.with that you can recognize the different button click

Shadow
no. this buttons are created in run time. may be, number of buttons will go up to 100 also. so i can't write different slot for each and every thing. i need to use the single slot to receive this.
saravanan
these buttons are of same class name or different? if you are having different class name then use QString className = current->metaObject()->className(); to get the class name for differentiation
Shadow
also, keep an ID in button to differentiate each button and check it in slot to get to know which button emitted the signal.
Shadow
A: 

You can use the QObject::Sender function to find which QObject sends the signal. Regarding the first warning in the doc, it's what you are searching for.

Patrice Bernassola
DO NOT use QObject::Sender, this is breaking the OOP principle of Qt
penguinpower
it's working. i have one more question. how to bring the object into front? because i have lot of widgets is there. when i click any one i need to bring into front. now i got which widget is clicked but, i can;t able bring to front. you have idea how to do that.
saravanan
+1  A: 

QSignalMapper is whatyou are searching for. You can add with QSignalMapper something like an Id (or even the QButton Pointer itself) as additional data to the signal emittance and you have to adjust your slots so it takes additional data (ID or Pointer).

Then either distinguish in the slot itself by the id your give your objects some virtual function type() so you can distinguish with that or even trycasting and catch errors (Tip: don't try the last method, it may work differntly on different compiler).

penguinpower
In the button click signal, i passed the button id to find the which button clicked. it's working fine. i have one more question. how to bring the object into front? because i have lot of widgets is there. when i click any one i need to bring into front. now i got which widget is clicked but, i can;t able bring to front. you have idea how to do that.
saravanan
What do you mean by bring to front? Focus?
penguinpower
Bring to front in the sense, the controls are overlapped. when i click the control, it's should come to front. the control fully visible from the overlap state.
saravanan
create a new question with a screenshot as this seems to be something *bigger* I still don't get the point.Don't forget to accept a answer as your initial question seems solved.
penguinpower