[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);
myMenu.addAction(OpenPublishAction);connect(OpenPublishAction, SIGNAL(triggered()),m_SignalMapper, SLOT(map()) );
m_SignalMapper->setMapping(OpenPublishAction,groupID);
connect(m_SignalMapper, SIGNAL(mapped(QString)), this, SLOT(OpenPublishWin(QString)));QAction* selectedItem = myMenu.exec(globalPos);
}
void MyApp::OpenPublishWin(QString gid)
{
WRITELOG(gid)
}@ -
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.
-
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()) );
signalMapper->setMapping(cutAct,groupID);
connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(OpenPublishWin(QString)));}
void MainWindow::OpenPublishWin(QString grpID)
{
// debugger stop here}@
-
-
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.)