简体   繁体   中英

QSignalMapper does not trigger SLOT

I'm trying to use QSignalMapper with my buttons, but I can't seem to get it to work to trigger my slot.

Here is my code for the SignalMapper:

In my header:

...
private:

    QSignalMapper *mapper;

In my cpp:

//Constructor:
mapper = new QSignalMapper(this);

//Init function, called by the constructor
connect(mapper, SIGNAL(mapped(int)), this, SLOT(HandleSignalEvents(int)));

Here is the code for connecting my button to it:

connect(m_ui->addEntryButton, SIGNAL(clicked()), mapper, SLOT(map()));
mapper->setMapping(m_ui->addEntryButton, 1);

Both connects are returning true as value, so the connects should be setup correctly. However, this slot is not being triggered at all:

public slots:

    void HandleSignalEvents(int);

----------------------------------------

void UIController::HandleSignalEvents(int param)
{
    //Do something    
}

I get no error whatsoever, so I can't really figure out what the problem ist. Both connects seem to work as usual, but the action does not trigger.

What could possibly go wrong here?

EDIT:

Note that m_signalMapper is a private member value of the class.

EDIT2:

Due to demand, here is my whole constructor chain:

UIController::UIController(Ui::TodoListerClass &ui)
{
    //Unrelated initializations
    mapper = new QSignalMapper(this);
    Init();
}

void UIController::Init()
{
    connect(mapper, SIGNAL(mapped(int)), this, SLOT(HandleSignalEvents(int)));

    InitUI();
    InitBinds();
}

void UIController::InitBinds()
{
    connect(m_ui->addEntryButton, SIGNAL(clicked()), mapper, SLOT(map()));
    mapper->setMapping(m_ui->addEntryButton, 1);
}

Edit 2:

More code:

Main:

#include "todolister.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TodoLister w;
    w.show();
    return a.exec();
}

TodoLister(.h):

class TodoLister : public QMainWindow
{
    Q_OBJECT

public:
    TodoLister(QWidget *parent = 0);
    ~TodoLister();

private: 
    Ui::TodoListerClass ui;
};

TodoLister(.cpp):

TodoLister::TodoLister(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    UIController uiController(ui);
}
TodoLister::TodoLister(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    UIController uiController(ui);
}

Your uiController object goes out of scope when the constructor returns, so can't emit signals afterwards because it is deleted.

EDIT

You are defining the shared pointer in the stack, not in the heap, so if you probably declared it in the constructor, the variable will get deleted when it finishes, not being accesible through out the rest of the program. Define it as a private variable of the class.

OLD In the documentation of QSignalMapper, the example of connect is at follows:

connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));

whilst you have signalMapper.get(). That parameter corresponds to the OBJECT receiving the signal, not a method of the object, so you should try signalMapper alone.

The same applies to the first connect, where you have specified the OBJECT generating the signal as signalMapper.get() , instead of signalMapper

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM