How to execute a function when a tray icon menu action is pressed?
Asked Answered
B

1

3
int main( int argc, char* argv[] )
{
    QApplication oApp( argc, argv );

    QAction *action1;
    QMenu menu;

    QSystemTrayIcon TrayIcon( QIcon("favicon.ico") );

    TrayIcon.show();

    action1= new QAction("action1", NULL);

    action1->setStatusTip("Create a new file");


    menu.addAction(action1);
    TrayIcon.setContextMenu(&menu);

    return oApp.exec();
}

How can I make it so that when I open the menu and press on action1, a function is executed?

Book answered 21/6, 2010 at 12:58 Comment(0)
W
4

Create new class (derived from QObject) with a slot called, e.g. myslot, then:

class MyClass : public QObject {
Q_OBJECT
...
public slots:
    void mySlot();
};

myObject = new MyClass();
connect(action1, SIGNAL(triggered()), myObject, SLOT(mySlot()));
Woodruff answered 21/6, 2010 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.