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 defini开发者_如何转开发ng element of each button).
Thanks in advance!
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.
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.
精彩评论