July 14, 2011

umen242 umen242
Ant Farmer
328 posts

[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 .

  1. void MyApp::ShowContextMenu(const QPoint& pos) // this is a slot
  2. {
  3.     QString groupID;
  4.     QPoint globalPos = ui.treeView_mainwindow->mapToGlobal(pos);
  5.     QModelIndex modelIndx = ui.treeView_mainwindow->indexAt(pos);
  6.     groupID = modelIndx.model()->index(modelIndx.row(),0,modelIndx.parent()).data(Qt::UserRole).toString();
  7.  QMenu myMenu;
  8.   OpenPublishAction = new QAction(tr("Send"), this);
  9.  myMenu.addAction(OpenPublishAction);
  10.  
  11.  connect(OpenPublishAction, SIGNAL(triggered()),m_SignalMapper, SLOT(map()) );
  12.  m_SignalMapper->setMapping(OpenPublishAction,groupID);
  13.  connect(m_SignalMapper, SIGNAL(mapped(QString)), this, SLOT(OpenPublishWin(QString)));
  14.  
  15.     QAction* selectedItem = myMenu.exec(globalPos);
  16.    
  17. }
  18. void MyApp::OpenPublishWin(QString gid)
  19. {
  20.  WRITELOG(gid)
  21. }

8 replies

July 14, 2011

koahnig koahnig
Mad Scientist
2114 posts

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.

July 14, 2011

changsheng230 changsheng230
Lab Rat
128 posts

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.

 Signature 

Chang Sheng
常升

July 14, 2011

umen242 umen242
Ant Farmer
328 posts

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:

July 14, 2011

changsheng230 changsheng230
Lab Rat
128 posts

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().

  1.  
  2. void MainWindow::MainWindow()
  3. {  
  4.     ....
  5.     QString groupID("1234");
  6.     QSignalMapper *signalMapper = new QSignalMapper(this);
  7.     connect(cutAct, SIGNAL(triggered()),signalMapper, SLOT(map()) );
  8.     signalMapper->setMapping(cutAct,groupID);
  9.     connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(OpenPublishWin(QString)));
  10.  
  11.  
  12. }
  13.  
  14. void MainWindow::OpenPublishWin(QString grpID)
  15. {
  16.     // debugger stop here
  17.    
  18. }

 Signature 

Chang Sheng
常升

July 14, 2011

ludde ludde
Ant Farmer
325 posts

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.

July 14, 2011

umen242 umen242
Ant Farmer
328 posts

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

July 14, 2011

ludde ludde
Ant Farmer
325 posts

The line

  1. 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.)

July 14, 2011

umen242 umen242
Ant Farmer
328 posts

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

 
  ‹‹ What is the best way to build flouting menu that stickes to the left side of the desktop?      Want to move a entire column in QtableWidget from Ist to Second position.. ››

You must log in to post a reply. Not a member yet? Register here!