Nominate our 2022 Qt Champions!

[Moved] Use of QSignalMapper

  • Hello,

    I have a problem using QSignalMapper. it does not work and I don't know why

    signalMapper = new QSignalMapper(this);
    signalMapper->setMapping(ui.dcPathSetButton, 1);
    connect(ui.dcPathSetButton, SIGNAL (clicked()), signalMapper, SLOT (map()));
    connect(signalMapper, SIGNAL (mapped(int)), this, SIGNAL (getPath(int)));

    this does work:
    connect(ui.dcPathSetButton, SIGNAL (clicked() ),this,SLOT (getPath() ) );

    comment: I have two functions: getPath() and getPath(int)

    Is there any idea what can be the problem here?

  • Your second connect call is wrong. You are trying to connect a signal to a signal whereas the target should be a slot:

    connect(signalMapper, SIGNAL (mapped(int)), this, SLOT(getPath(int)));

    Running with QT_FATAL_WARNINGS=1 catches this kind of thing.

  • One should be carful in this case. It is perfectly ok to connect a signal to another signal (instead of a slot). In this case once the first signal is fired, the second is fired too (see the example of "QObject::connect() ":

    It is very important, though, to use one of the correct macros SIGNAL(...) or SLOT(...) to wrap the method. You cannot add as method declared as slot to the SIGNAL macro and vice versa. I think that's what you did, and - even worse - you have a method with the same name, but different arguments declared one time as a slot and one time as a signal. I would change this, as it leads to confusion in the future.

Log in to reply