user2366975
user2366975

Reputation: 4700

Qt5 C++ connect triggered QAction to a member function

Why can't I connect the action to call the function if triggered? I thought that I've understood the syntax according to this source that states one can call a function directly.

void Traymenu::createMainContextMenu(){
    QAction *actionNewNote = mainContextMenu.addAction("Test Func");

    QObject::connect(actionNewNote,QAction::triggered,Traymenu::testFunc);

    mainIcon.setContextMenu(&mainContextMenu);

}

void Traymenu::testFunc(){
    printf("test func");
}

Error:invalid use of non-static member function 'void QAction::triggered(bool)

                                         ^

Upvotes: 3

Views: 2035

Answers (1)

RA.
RA.

Reputation: 7777

You need to pass pointers to functions into connect. You also need to pass in a pointer to the receiving object:

QObject::connect(actionNewNote, &QAction::triggered, this, &Traymenu::testFunc);

Note the "&" prior to QAction::triggered.

Upvotes: 6

Related Questions