简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM