Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get calling button from a clicked event

I'm trying to make an small gui to deploy .ear and .war files on my local glassfish installation. SO i have made five rows containing a file name field, a checkbox and a button to bring up a file dialogbox to locate the war/ear file. It would be nice to have all buttons call the same function and from the function sort out which of the five buttons who made the call ( to update the correct text fields ). Don't know if this is the intended way of doing it in an object oriented way but my only gui programming experience is some old win16 event loops :).

//BRG Anders Olme

like image 219
Buzzzz Avatar asked Mar 04 '11 06:03

Buzzzz


People also ask

How do you call a button click?

Method 1: Use addEventListener() Method Get the reference to the button. For example, using getElementById() method. Call addEventListener() function on the button with the “click” action and function passed as arguments.

How do you call an event on button?

To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

How do you call a button click event in jQuery?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.


2 Answers

QPushButton *buttonA = new QPushButton("A");
QPushButton *buttonB = new QPushButton("B");
QPushButton *buttonC = new QPushButton("C");

buttonA->setObjectName("A");
buttonB->setObjectName("B");
buttonC->setObjectName("C");

connect(buttonA, SIGNAL(clicked()), this, SLOT(testSlot()));
connect(buttonB, SIGNAL(clicked()), this, SLOT(testSlot()));
connect(buttonC, SIGNAL(clicked()), this, SLOT(testSlot()));

//Now in slot implementation
void QWidget::testSlot()
{
  QObject *senderObj = sender(); // This will give Sender object
  // This will give obejct name for above it will give "A", "B", "C"
  QString senderObjName = senderObj->objectName(); 

  if(senderObjName == "A")
  {
   //Implement Button A Specific 
  }
  //Similarly for "B" and "C"
  if(senderObjName == "B")
  {
   //Implement Button B Specific 
  }
  if(senderObjName == "C")
  {
   //Implement Button C Specific 
  }
}

I have used this method to implement such case because code is more readable but it may be time consuming as String comparison comes. Thank you!

like image 143
DigviJay Patil Avatar answered Sep 28 '22 12:09

DigviJay Patil


Connect each button's click() signal with one and the same slot and use QObject * QObject::sender () const [protected] in this slot to find out which button sent the signal (was clicked). Alternatively you could use QSignalMapper which is a special class made just for this kind of task.

like image 39
Piotr Dobrogost Avatar answered Sep 28 '22 12:09

Piotr Dobrogost