Home > Mobile > How to identify the clicked event?
How to identify the clicked event?
Time:09-27
As title, an interface program has a lot of pushbutton, if they clicked () signal is connected to the same slot, excuse me the slot in the clicked () response, which is how the difference between the pushbutton clicked event? Or, I think in a slot, for different button clicked perform similar but different operation parameters, is clicked without arguments, however, how do I how to pass the parameters? Thank you,
CodePudding user response:
There are two ways: 1, the use of qt in signal mapper QSignalMapper, you in the baidu search the name of the class have simple usage 2, you can be done through one more layer of abstraction, through subclassing to complete, at the time of create BUTTON by passing a parameter index, and assigned to the private members of the class, etc. Will click you can according to the index is know which BUTTON to trigger
The class MyPushButton: public QPushButton { Q_OBJECT Public: Explicit MyPushButton (int index, QWidget * parent=0) : QPushButton (parent), index (index) { The connect (this, SIGNAL (clicked ()), and this, SLOT (OnClicked ())); } Private: Int index; Public slots: Void OnClicked () {qDebug () & lt; };
Below a few lines of code is to create the BUTTON
for(int i=0; i <3; I++) { MyPushButton * button=new MyPushButton (I, this); The button - & gt; SetText (" button "+ QString: : number (I)); The button - & gt; Move (120 * I + 10, 100); The button - & gt; Show (); }
CodePudding user response:
1, in the tank, to signal the sender can be recognized, for example, we put the button2 and button3 bound to the tank Test (); The connect (UI - & gt; PushButton_2, SIGNAL (clicked (bool)), and this, SLOT (Test ())); The connect (UI - & gt; PushButton_3, SIGNAL (clicked (bool)), and this, SLOT (Test ()));
2, and then Test such written to OK: Void MainWindow: : Test () { QDebug () & lt; }