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. Cannot connect QAction in a QPushButton menu to a slot

Cannot connect QAction in a QPushButton menu to a slot

Scheduled Pinned Locked Moved Solved General and Desktop
qpushbuttonqactionqmenu
3 Posts 3 Posters 1.3k Views
  • 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.
  • F Offline
    F Offline
    fugreh
    wrote on last edited by
    #1

    Hi,

    I have a QPushButton to which I add a menu containing actions. When I run the application, upon clicking on the button, it expands and I see the actions that I have added (XML, CSV). But when I click on them the corresponding slot is not triggered. Why could this be?

    // CDeepLearningPanel declaration (.h)
    ...
    private slots:
        void saveLandmarksAsXML();
        void saveLandmarksAsCSV();
    ...
    
    // CDeepLearningPanel definition (.cpp)
    ...
    // CDeepLearningPanel constructor
        m_saveLandmarksMenu = std::unique_ptr<QMenu>(new QMenu);
        QAction *saveAsXML = m_saveLandmarksMenu->addAction("XML");
        QAction *saveAsCSV = m_saveLandmarksMenu->addAction("CSV");
        ui->pushButtonSaveAs->setMenu(m_saveLandmarksMenu.get());
    
        connect(saveAsXML, SIGNAL(triggered()), this, SLOT(saveLandmarksAsXML));
        connect(saveAsCSV, SIGNAL(triggered()), this, SLOT(saveLandmarksAsCSV));
    
    ....
    void CDeepLearningPanel::saveLandmarksAsXML() {
        qDebug() << "save Landmarks as XML";
    }
    
    void CDeepLearningPanel::saveLandmarksAsCSV() {
        qDebug() << "save Landmarks as CSV";
    }
    ...
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      MrShawn
      wrote on last edited by
      #2

      @fugreh said in Cannot connect QAction in a QPushButton menu to a slot:

      connect(saveAsXML, SIGNAL(triggered()), this, SLOT(saveLandmarksAsXML));

      From what I see, it should be triggered(bool), in which case you will need to declare saveLandmarksAsXML and saveLandmarksAsCSV to take bool inputs. Your line will then change to:

      connect(saveAsXML, SIGNAL(triggered(bool)), this, SLOT(saveLandmarksAsXML(bool)));
      

      Hope it helps.

      1 Reply Last reply
      5
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        In addition to @MrShawn, the slot can have less arguments than the signal, therefore you can keep their current signature.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2

        • Login

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