Cannot connect QAction in a QPushButton menu to a slot



  • 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";
    }
    ...
    


  • @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.


  • Lifetime Qt Champion

    Hi,

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