Nominate our 2022 Qt Champions!

[Solved] Problem with QSignalMapper and QAction never triger the Slot

  • Hi i try to bind slot with argument to QAction triggered SIGNAL
    i have this code ,the context menu working great . BUT the OpenPublishWin never triggered .

    @void MyApp::ShowContextMenu(const QPoint& pos) // this is a slot
    QString groupID;
    QPoint globalPos = ui.treeView_mainwindow->mapToGlobal(pos);
    QModelIndex modelIndx = ui.treeView_mainwindow->indexAt(pos);
    groupID = modelIndx.model()->index(modelIndx.row(),0,modelIndx.parent()).data(Qt::UserRole).toString();
    QMenu myMenu;
    OpenPublishAction = new QAction(tr("Send"), this);

    connect(OpenPublishAction, SIGNAL(triggered()),m_SignalMapper, SLOT(map()) );
    connect(m_SignalMapper, SIGNAL(mapped(QString)), this, SLOT(OpenPublishWin(QString)));

    QAction* selectedItem = myMenu.exec(globalPos);

    void MyApp::OpenPublishWin(QString gid)

  • Was the connect successful? You can check the return bool and have a look to the output window of your application. Qt reports if the signal and slot cannot be connected.

  • I did not see any problem of your usage of QSignalMapper. and I assume you initialized the "signalMapper = new QSignalMapper(this)" in the MyApp constructor.

  • koahnig : they both return true
    changsheng230 : yes indid i set it like you write with new QSignalMapper(this)
    still it never gets to OpenPublishWin.
    also i did put the OpenPublishWin(QString gid) under public slots:

  • I just make a try , adding the following test codes to a QMainWindow example, and the signalMapper works well, the debugger can stop at the break point of OpenPublishWin().

    void MainWindow::MainWindow()
    QString groupID("1234");
    QSignalMapper *signalMapper = new QSignalMapper(this);
    connect(cutAct, SIGNAL(triggered()),signalMapper, SLOT(map()) );
    connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(OpenPublishWin(QString)));


    void MainWindow::OpenPublishWin(QString grpID)
    // debugger stop here


  • I cannot see anything wrong with your code either, except perhaps that your signal mapper will contain more and more mappings... but maybe that's intentional? Anyway, it should only affect memory usage and eventually performance, not functionality.

  • what do you mean by more and more mapping ? how can i prevent this ?

  • The line @m_SignalMapper->setMapping(OpenPublishAction,groupID);@ adds a mapping to the mapper. It looks like you are using the same mapper every time the ShowContextMenu is called, so the mapper will contain more and more mappings. The OpenPublishAction variable is also reused, but it does not look like you are destroying the actions. This is fine if you want to add more and more actions to a menu, and have the corresponding mappings in the mapper - maybe that's what you are doing? In that case the code is probably fine. (It's a bit hard to tell what you are doing, since we cannot see the context in which this code is used.)

  • ok ludde after looking again in my code and moving the QAction to set only once , its solved my problem

Log in to reply