2

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)

tobilocker
  • 891
  • 8
  • 27

3 Answers3

2

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);
ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35
  • Thanks for the answer, but how will this pass `fileName` as an argument to `changePicture` when a `SIGNAL` like `activated` is emitted? – tobilocker Nov 02 '15 at 10:00
  • from your code, it looks like you already have `imageFileName` constructed from `xmlreader`. You aren't using `activated` signal there, right? – ramtheconqueror Nov 02 '15 at 10:04
  • OK, i will probably need to explain the background to this: I want a picture in a label only to be changed when a signal is emitted. For a `QPushButton` this would just be `clicked` but there is no such signal for a `QComboBox` (or?). So every combo instance would show another picture when it is being changed or whatever... – tobilocker Nov 02 '15 at 10:12
1

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.

agold
  • 6,140
  • 9
  • 38
  • 54
  • Yes this seems like bad programming anyway and i only shall implement those changes :) I will have to go through C++ inheritance while transiting from Python to C++ – tobilocker Nov 02 '15 at 10:17
  • And [Signals & Slots](http://doc.qt.io/qt-5/signalsandslots.html) reference is a bit short information on a bit larger topic, isn't it? – tobilocker Nov 02 '15 at 10:22
0

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()) );
tobilocker
  • 891
  • 8
  • 27