Passing a QString to a class method of parent with QSignalMapper

270 views Asked by At

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)

3

There are 3 answers

0
tobilocker On BEST ANSWER

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()) );
3
ramtheconqueror On

Some issues with your code.

  1. you really can't connect to a private slot of a QObject
  2. You are creating a new signal mapper every time you call readCombo which you aren't clearing - resulting memleaks.
  3. calling connect multiple times creates multiple connections i.e., you will call the same slot multiple times with single signal.

From your example code, I see that you can solve this either by making the parentClass slot public OR add a signal to the childClass and connect to it in the parentClass.

Other option is change the readCombo like this:

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

and your parent class as

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);
2
agold On

I see several errors:

  1. You are connecting to a private slot in parentClass from childClass, you should make it public if you want to connect it.
  2. You connect a signal to a slot with another function signature. Your signal has parameter type int, and slot type QString*. The functions should share the same type of parameters.
  3. In your connect you refer use the parameter type QString* in your slot, but in parentClass the parameter type is QString.

For more information about signals and slots see: Signals & Slots.

PS: your naming of childClass and parentClass is not clear either since they both inherit from QObject. See C++ inheritance.