km2442
km2442

Reputation: 827

Connect QAction to nonmember-qt function in Qt5

I want to add a button to my TrayIcon (in Qt5.5) . (I'm using QMenu, QAction) When I click it I would like to call public Non-Qt function. How to connect to this SIGNAL?

mainwindow.h:
private:
    void tray();
    QMenu *trayIconMenu;
    QAction *ExampleAction;

mainwindow.cpp:

    void exfunction()
    {
    }

    void MainWindow::tray()
    {
        ExampleAction = new QAction(tr("Sample Text"), this);
        connect(ExampleAction,SIGNAL(triggered()), exfunction()); //How to propertly connect it? 

        trayIconMenu = new QMenu(this);
        trayIconMenu->addAction(ExampleAction);
    }

Upvotes: 1

Views: 624

Answers (1)

dtech
dtech

Reputation: 49289

In Qt 5 you can connect to a regular member function, to a free function or a lambda expression, but you must use the new syntax

connect(ExampleAction, &QAction::triggered, exfunction);

Upvotes: 7

Related Questions