Important: Please read the Qt Code of Conduct -

[SOLVED] signalMapper - mapped SIGNAL with custom Widgets

  • Hi,

    I'm trying connect a single dial with several custom widgets, according with which actual widget has been selected. Only one custom widget at a time can be connected to the dial.
    I've succesfully solved it with signalMapper as referenced in "signalMapper documentation":

    Here is my code:
    QSignalMapper *signalMapper = new QSignalMapper(this);
    signalMapper->setMapping(widgetTest, (QWidget)widgetTest);
    signalMapper->setMapping(widgetTest2, (QWidget)widgetTest2);

    connect(widgetTest, SIGNAL(mouseClickEvent()),
            signalMapper, SLOT (map()));
    connect(widgetTest2, SIGNAL(mouseClickEvent()),
            signalMapper, SLOT (map()));
    connect(signalMapper, SIGNAL(mapped(QWidget*),
            this, SLOT(slotMainWindow(QWidget*)));
    lastActiveWidget = NULL;


    void MainWindow::slotMainWindow(QWidget *actualActiveWidget){

        qDebug("actualActiveWidget != NULL");
        disconnect(dial, SIGNAL(valueChanged(int)),lastActiveWidget,SLOT(setMainValue(int)));
    lastActiveWidget = actualActiveWidget;
    connect(dial, SIGNAL(valueChanged(int)),actualActiveWidget,SLOT(setMainValue(int)));


    My doubt is about the mapped SIGNAL: it is only possible to have int, Qstring, QObject* or QWidget* as arguments, but i need to have MyCustomWidget as an argument. Because MyCustomWidget inherits QWidget, it is possible to have it running treating MyCustomWidget as a QWidget for the signals, but although it works, I am not 100% sure if it's correct to work like that, having consecuences in the future.

    Does anybody knows if these approach is ok or should try another way?

    Thanks in advance.

  • Lifetime Qt Champion


    That's the correct usage of QSignalMapper. One thing though, don't use c-style caste with QObjects, you have qobject_cast for that. But in this case, you don't need any cast at all

  • Thanks SGaist for the answer and for the advice, appreciate it a lot!.



Log in to reply