Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9078667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:40:56+00:00 2026-06-16T19:40:56+00:00

I have a QMenu with many submenus. These are dynamically created i.e. the names

  • 0

I have a QMenu with many submenus. These are dynamically created i.e. the names menus come from a db and created in a loop. Now i wanted to fire the same slot triggered() or similar when a menu is clicked, but i needed the QString menu name to be passed to slot so i could perform menu specific actions. I have tried this i.e. passing a QAction * to the triggered event and used setData but i am getting the run time error.

object::connect: No such signal QAction::triggered(QAction *)

for(int j=0; j<channelTypes[i].getNumChannels() ; j++){
        QAction *subMenuAct = subMenu->addAction(tr(c_name)); // c_name the menu name
        subMenuAct->setData(ch_name);
        connect(subMenuAct, SIGNAL(triggered(QAction *)), this, SLOT(playChannel(QAction *))); // playChannel is the slot
}

void <ClassName>::playChannel(QAction *channelAction)
{
     QString str = channelAction->data().toString();
    qDebug() << "Selected - " << str;
}

Alternately, i have also tried QSignalMapper where signalMapper is a data member initialized in the constructor

signalMapper = new QSignalMapper(this);

and

for(int j=0; j<channelTypes[i].getNumChannels() ; j++){
       QAction *subMenuAct = subMenu->addAction(tr(c_name));

       connect(subMenuAct, SIGNAL(triggered()), signalMapper, SLOT(map()));
       signalMapper->setMapping(subMenu, ch_name);
       connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(playChannel(QString)));
}

In the second case, i don’t get any error, however the slot function playChannel is not being called. Would really appreciate if some one could help resolving it.

Update 1: The only difference that i see from other examples i have seen is that usually people are connecting signals from several widgets to a single slot (say different buttons). In my case i am connecting several sub menus (with different names) to a single slot. Should this make any difference?

Update 2: It worked after the correction suggested in the solution below for QSignalMapper. Also the fact that i was using SubMenu as argument to setMapping , where as MenuAction item should have been used instead. But now i am getting event fired multiple times i.e. as many times as there are entries in the main menu for the selected sub menu category. If channel type is English (main menu) with four entries), HBO, star movies etc. (sub menu), and i choose HBO, then event is fired four times with string HBO. It works fine if i create a separate signal mapper for each submenu. But i was hoping a single mapper should be used and i am doing something incorrectly here. Some more details in the comments to the answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:40:57+00:00Added an answer on June 16, 2026 at 7:40 pm

    After adding the QAction to the menu, you only have to connect QMenu to the slot. You don’t connect each action individually to the slot:

    for(int j=0; j<channelTypes[i].getNumChannels() ; j++){
        ch_name = <name from the database for the channel j>;
        QAction *subMenuAct = subMenu->addAction(tr(ch_name));
        subMenuAct->setData(ch_name);
    }
    
    connect(subMenu, SIGNAL(triggered(QAction *)), 
            this, SLOT(playChannel(QAction *)), Qt::UniqueConnection);
    

    As I don’t know how you if you delete subMenu each time the dynamic menu is filled, the Qt::UniqueConnection ensure that the slot won’t be reconnected multiple times.


    For the signal mapper version, you should only connect the actions to the mapper in the loop. The connection from the mapper to the slot should only be done once.

    for(int j=0; j<channelTypes[i].getNumChannels() ; j++){
       ch_name = <name from the database for the channel j>;
       QAction *subMenuAct = subMenu->addAction(tr(ch_name));
       connect(subMenuAct, SIGNAL(triggered()), signalMapper, SLOT(map()));
       signalMapper->setMapping(subMenuAct, ch_name);   
    }
    connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(playChannel(QString)));
    

    And for that case, the slot playChannel should accept a QString instead of a QAction*.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that is a single QGlwidget with a right-click QMenu created
I have a QMenu which has several menu items built dynamically. To this end,
have a nice day. I got problem when trying to create an image from
I have implemented contextual menus in QTreeView items with the following code MyDerivedQTreeView->setModel(MyDerivedQAbstractItemModel); MyDerivedQTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
Have been trying to figure this problem out for a while now and was
I am using QT4 and dynamically adding entries to a QMenu. Is it possible
Have a photography site that I want to prevent image copying from. How can
Have created a ATL COM project through which I am inserting Menu Items to
i have QMenu that holds sub menu i like to be able to update
Have created an android test project and currently trying to write android unit tests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.