c++qtqsignalmapper

Passing a QString to a class method of parent with QSignalMapper


I have a parent class with its method to change a labels picture on certain Signals. E.g. When something happens in a QComboBox... (valueChanged, activated)

    class parentClass : public QMainWindow
    {
        Q_OBJECT
        ...

    public slots:
        //this is the slot i want to connect to some signal of
        //e.g a combo box to change the picture by passed in string 
        void changePicture(QString fileName);

Then i have this child:

    class childClass : public QObject
    {
        Q_OBJECT
    public:
        childClass(parentClass *parent, QTabWidget *tab, QStringList *guards=0);

    private:
        bool readCombo(QXmlStreamReader *xmlreader);

Now inside of readCombo i want to read a string and pass it to change picture:

    QString imageFileName = xmlreader->attributes().value("image").toString(); 

    QSignalMapper * signalMapper = new QSignalMapper(parent);

    //this is just one of many trials to get this working, i hope you get the picture

    connect(combo , SIGNAL(activated(int)), parent, SLOT(changePicture(QString *)));

    signalMapper->setMapping(combo, imageFileName);

But this gives me either No such Signal , No such Slot or in the upper case Incompatiple sender/receiver arguments

I would appreciate some help on this one since the syntax is really not being intuitive (imo) and i can't find any good reference that is working for my case (did trial and error a lot before asking)


Solution

  • OK, got it:

    QString imageFileName = xmlreader->attributes().value("image").toString(); 
    
    QSignalMapper * signalMapper = new QSignalMapper(parent);
    
    signalMapper->setMapping(combo, imageFileName);
    
    connect(signalMapper, SIGNAL(mapped(QString)), parent, SLOT(changePicture(QString)));
    
    connect( combo, SIGNAL(activated(int)), signalMapper, SLOT(map()) );