记录qt的一些案例,以后方便查找
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include "menudialog.h"
  4. #include <QDebug>
  5. #include <QPushButton>
  6. #include <QSystemTrayIcon>
  7. #include <QMenu>
  8. #include <QAction>
  9. #include <QDebug>
  10. #include <QWidgetAction>
  11. Widget::Widget(QWidget *parent) :
  12. QWidget(parent),
  13. ui(new Ui::Widget)
  14. {
  15. ui->setupUi(this);
  16. setWindowFlags(Qt::Tool);
  17. CreateSystemTrayIcon(); //创建托盘
  18. }
  19. Widget::~Widget()
  20. {
  21. delete ui;
  22. }
  23. void Widget::CreateSystemTrayIcon()
  24. {
  25. QWidgetAction *pWdtAction = new QWidgetAction(this);
  26. MenuDialog * menudialog = new MenuDialog();
  27. pWdtAction->setDefaultWidget(menudialog);
  28. //初始化菜单并添加项
  29. QMenu* trayMenu = new QMenu(this);//菜单
  30. trayMenu->addAction(pWdtAction);
  31. //创建一个系统托盘
  32. QSystemTrayIcon* trayIcon = new QSystemTrayIcon(this);
  33. trayIcon->setIcon(QIcon("://app.ico")); //设置托盘图标
  34. trayIcon->setToolTip("Shell_Notify"); //鼠标放上去显示的东东
  35. trayIcon->setContextMenu(trayMenu); //设置菜单
  36. trayIcon->show();
  37. connect(trayIcon, &QSystemTrayIcon::activated, this, [=](QSystemTrayIcon::ActivationReason reason)
  38. {
  39. menudialog->uodateNumbers();
  40. if( reason == 3){ //鼠标左键
  41. trayIcon->contextMenu()->exec(QCursor::pos());
  42. }
  43. });
  44. }