简体   繁体   English

QSignalMapper 和 QAction 的问题永远不会触发 Slot

[英]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.嗨,我尝试将带有参数的插槽绑定到 QAction 触发的信号我有这个代码,上下文菜单工作得很好。 BUT the OpenPublishWin never triggered.但 OpenPublishWin 从未触发。

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)
}

A quick look at the Qt docs for QSignalMapper (assuming that is what you're using based on the question title) states that the parameter for the mapped signal is const QString&.快速查看 QSignalMapper 的Qt文档(假设这是您根据问题标题使用的)指出映射信号的参数是 const QString&。 I can't recall if the parameter needs to be exact in this case for the connection but it may be a factor.我不记得在这种情况下参数是否需要准确用于连接,但这可能是一个因素。

Additionally, double check that your connects are being made by wrapping them in an assert or some form of verify.此外,通过将它们包装在断言或某种形式的验证中来仔细检查您的连接是否正在建立。 Qt will also print out to the console if a connection cannot be made.如果无法建立连接,Qt 也会打印到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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