How to save source signal parameters when using QSignalMapper?

I ran into a problem that I need to save the parameters of the displayed source signal. So far, I have only found examples for displaying signals without any parameters. For example, the signal clicked ():

signalMapper = new QSignalMapper(this); signalMapper->setMapping(taxFileButton, QString("taxfile.txt")); connect(taxFileButton, SIGNAL(clicked()), signalMapper, SLOT (map())); connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(readFile(QString))); 

However, I will need to display some signal with its own parameters, for example, by clicking (bool), then SLOT should have two doStuff arguments (bool, QString):

 connect(taxFileButton, SIGNAL(clicked(bool)), signalMapper, SLOT (map())); connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(doStuff(bool,QString))); 

However, this does not work? Is there any work around?

Thanks!

+7
source share
1 answer

QSignalMapper does not provide functions for passing signal parameters.

see documentation:
This class collects a set without signal parameters and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal.

There are ways to solve this:

If Qt4 is used , then I would suggest implementing your own map calculator, which supports the parameters you need.
QSignalMapper implementation will be a good example to start with.

But , if Qt5 is used , you can do exactly what you need without using QSignalMapper at all. Just connect the signal to lambda:

 connect(taxFileButton, &TaxFileButton::clicked, [this](bool arg) { doStuff(arg, "taxfile.txt"); } ); 

I assume that taxFileButton is an instance of the taxFileButton class.

If C ++ 11 lambda is not suitable for any reason, then tr1::bind can be used to bind this and "taxfile.txt" values.
Note that such a connection will not be automatically disconnected if the this object is destroyed. More details here .

+7
source

All Articles