Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Problem with QSignalMapper and QAction never triger the Slot
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 6.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    umen242
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        changsheng230
        wrote on last edited by
        #3

        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.

        Chang Sheng
        常升

        1 Reply Last reply
        0
        • U Offline
          U Offline
          umen242
          wrote on last edited by
          #4

          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:

          1 Reply Last reply
          0
          • C Offline
            C Offline
            changsheng230
            wrote on last edited by
            #5

            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

            }@

            Chang Sheng
            常升

            1 Reply Last reply
            0
            • L Offline
              L Offline
              ludde
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • U Offline
                U Offline
                umen242
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  ludde
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • U Offline
                    U Offline
                    umen242
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved