Connect QMenu :: addAction directly to lambda (function signature mismatch)

How to connect QActiondirectly to a lambda slot?

QMenu m;

AT:

QAction newSubfolder(QIcon(":/icons/newfolder.png"),tr("New Subfolder"),&m);
m.addAction(&newSubfolder);
connect(&newSubfolder,&QAction::triggered,
        this,[this,p](){qDebug()<<"New Subfolder";});

Does not work:

m.addAction(QIcon(":/icons/newfolder.png"),tr("New Subfolder"),
                [this,p](){qDebug()<<"New Subfolder";});

Error:

No matching function for call to 'QMenu::addAction(QIcon, QString, FolderMenuWidget::showContextMenu(QPoint)::__lambda0)'
                 [this,p](){qDebug()<<"New Subfolder";});
                                                       ^

Yes I see an error message, but I don’t understand what I need to change for the lambda slot. It also does not work if I add a pointer to an element thisbefore the lambda.

This is in Qt 5.3.

+4
source share
1 answer

The overload addActionyou are looking for was added in Qt 5.6.

+3
source

Source: https://habr.com/ru/post/1661230/


All Articles