Cenoc
Cenoc

Reputation: 11662

Obtaining the Button Clicked after a Clicked() is emitted in Qt (C++)

I was wondering, once a clicked() is emitted by a button is there any way of finding out what button emitted it without overloading the click() function? (I have a bunch of buttons with almost the same function but different text, which is the defining element of each button).

Thanks in advance!

Upvotes: 3

Views: 1444

Answers (2)

blwy10
blwy10

Reputation: 4892

Within your slot, you can call the sender() function to get the QObject that sent you the clicked() signal. It returns a QObject *. Use qobject_cast to cast the QObject * to QPushButton *.

Documentation here.

Upvotes: 10

Troubadour
Troubadour

Reputation: 13421

You probably want to use a QSignalMapper.

In your case, if it's only the text you are interested in then connect the clicked() signal on each button to the map() slot on your signal mapper and then set a string mapping with setMapping( QObject * sender, const QString & text ). The signal mapper will then re-emit the signal in the form of it's own mapped( const QString & text ) signal with the correct text for the button that was clicked.

Upvotes: 6

Related Questions