c++ qt tray icon menu action

帅比萌擦擦* 提交于 2019-12-24 01:09:50

问题


I got this code in qt creator;

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();
}

but how can i make that when i open the menu and press on action1 that it execute a function?

thnx very much!


回答1:


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()));


来源:https://stackoverflow.com/questions/3084825/c-qt-tray-icon-menu-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!