Passing an argument to a slot

I want to override mouseReleaseEvent with a bunch of QActions and QMenus ...

connect(action1, SIGNAL(triggered()), this, SLOT(onStepIncreased())); connect(action5, SIGNAL(triggered()), this, SLOT(onStepIncreased())); connect(action10, SIGNAL(triggered()), this, SLOT(onStepIncreased())); connect(action25, SIGNAL(triggered()), this, SLOT(onStepIncreased())); connect(action50, SIGNAL(triggered()), this, SLOT(onStepIncreased())); 

So, I want to pass an argument to the onStepIncreased slot (as you can imagine, this is 1,5,10,25,50). Do you know how I can do this?

+64
qt qsignalmapper
Mar 01 '11 at 10:20
source share
5 answers

Use QSignalMapper . Like this:

 QSignalMapper* signalMapper = new QSignalMapper (this) ; connect (action1, SIGNAL(triggered()), signalMapper, SLOT(map())) ; connect (action5, SIGNAL(triggered()), signalMapper, SLOT(map())) ; connect (action10, SIGNAL(triggered()), signalMapper, SLOT(map())) ; connect (action25, SIGNAL(triggered()), signalMapper, SLOT(map())) ; connect (action50, SIGNAL(triggered()), signalMapper, SLOT(map())) ; signalMapper -> setMapping (action1, 1) ; signalMapper -> setMapping (action5, 5) ; signalMapper -> setMapping (action10, 10) ; signalMapper -> setMapping (action25, 25) ; signalMapper -> setMapping (action50, 50) ; connect (signalMapper, SIGNAL(mapped(int)), this, SLOT(onStepIncreased(int))) ; 
+106
Mar 01 2018-11-11T00:
source share

With Qt 5 and the C ++ 11 compiler, the idiomatic way to do such things is to give the functor a connect :

 connect(action1, &QAction::triggered, this, [this]{ onStepIncreased(1); }); connect(action5, &QAction::triggered, this, [this]{ onStepIncreased(5); }); connect(action10, &QAction::triggered, this, [this]{ onStepIncreased(10); }); connect(action25, &QAction::triggered, this, [this]{ onStepIncreased(25); }); connect(action50, &QAction::triggered, this, [this]{ onStepIncreased(50); }); 

The third argument to connect nominally optional. It is used to set the context of the stream in which the functor will be executed. This is always necessary when a functor uses an instance of QObject . If a functor uses multiple instances of QObject , they must have some common parent that controls their lifetime, and the functor must reference this parent or must ensure that objects overflow the functor.

On Windows, this works on MSVC2012 and later.

+87
Mar 14 '14 at 16:57
source share

The QObject::sender() function returns a pointer to the object that signaled the slot. You can use this to find out what action was triggered.

+9
Mar 01 '11 at 10:23
source share

Perhaps you can subclass QAction with the m_increase member variable.
Connect the trigger signal () to the slot in the new QAction subclass and emit a new signal (e.g., triggered (int number)) with the correct parameter.
eg

 class MyAction:public QAction { public: MyAction(int increase, ...) :QAction(...), m_increase(increase) { connect(this, SIGNAL(triggered()), this, SLOT(onTriggered())); } protected Q_SLOTS: void onTriggered() { emit triggered(m_increase); } Q_SIGNALS: void triggered(int increase); private: int m_increase; }; 
+1
Mar 01 '11 at 11:00
source share
 QVector<QAction*> W(100); W[1]= action1; W[5]= action5; W[10]= action10; W[25]= action25; W[50]= action50; for (int i=0; i<100; ++i) { QSignalMapper* signalmapper = new QSignalMapper(); connect (W[i], SIGNAL(triggered()), signalmapper, SLOT(map())) ; signalmapper ->setMapping (W[i], i); connect (signalmapper , SIGNAL(mapped(int)), this, SLOT(onStepIncreased(int))); } 
0
Feb 23 '18 at 9:58
source share



All Articles