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; }

CodePudding user response:

reference 1st floor qq423399099 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 ();
}

Thank you for your answer, by the way, their definition of how to better control on the mainwindow layout? Be sure to use the layout control? Could you like easily drag control in QT designer? I am a novice qt, many don't understand,

CodePudding user response:

reference u012436426 reply: 3/f
Quote: refer to 1st floor qq423399099 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 ();
}

Thank you for your answer, by the way, their definition of how to better control on the mainwindow layout? Be sure to use the layout control? Could you like easily drag control in QT designer? I am a novice qt, many don't understand,

No layout can also ah, but need to test the LZ UI language like that, the size of the control, put, white space, background, decoration

CodePudding user response:

Void GasolineWindow: : on_toolButton_clicked ()
{
QToolButton * button=(QToolButton *) sender ();
If (button==toolbuttonSet)
{
//...

}
Else if (button==toolbuttonSave)
{
}
Else if (button==toolbuttonBack)
{

}
}

CodePudding user response:

Add button to QButtonGroup button group, so that each button will have corresponding number,
QButtonGroup click have clicked (int) signal, int is the serial number of button,
As long as the switch inside groove function number can do event for the button

CodePudding user response:

The answer is very good, 1/f, 7 floor helped me,
For 2019 years, give an update now,
QSignalMapper this class is marked as deprecated, here (https://zhuanlan.zhihu.com/p/62190512) and the official documentation lambda (https://doc.qt.io/qt-5/qsignalmapper.html) are recommended to be used to replace,
  •  Tags:  
  • Qt
  • Related