dialog.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #ifndef DIALOG_H
  2. #define DIALOG_H
  3. #include "mythread.h"
  4. #include <QDialog>
  5. #include "serialport/serailport.h"
  6. #include "hkcamera/hkcamera.h"
  7. #include<QMessageBox>
  8. #include <QCloseEvent>
  9. #include <QSystemTrayIcon>
  10. #include <QMenu>
  11. #include <QAction>
  12. #include <QVBoxLayout>
  13. #include <QPushButton>
  14. #include <QApplication>
  15. namespace Ui {
  16. class Dialog;
  17. }
  18. class Dialog : public QDialog
  19. {
  20. Q_OBJECT
  21. public:
  22. explicit Dialog(QWidget *parent = nullptr);
  23. ~Dialog();
  24. //这是虚函数,重写这个虚函数,当你按下窗口右上角的"×"时,就会调用你所重写的此函数.
  25. void closeEvent(QCloseEvent* e);
  26. //protected:
  27. // void showEvent(QShowEvent *event) override {
  28. // QDialog::showEvent(event); // 调用基类的showEvent
  29. // this->showMinimized(); // 在对话框显示后立即最小化
  30. // }
  31. private:
  32. QSystemTrayIcon *trayIcon; // 在这里声明trayIcon
  33. public:
  34. void startInBackground() {
  35. // 设置对话框的布局和控件(这里只添加一个按钮作为示例)
  36. // auto *layout = new QVBoxLayout(this);
  37. // auto *button = new QPushButton("Hello, Tray!", this);
  38. // layout->addWidget(button);
  39. // 隐藏对话框
  40. hide();
  41. // 创建托盘图标
  42. trayIcon = new QSystemTrayIcon(this);
  43. trayIcon->setIcon(QIcon("D:/irdet/cfg/ir.jpg")); // 设置图标
  44. trayIcon->setToolTip("红外缺陷检测"); // 设置提示信息
  45. // 创建托盘图标的上下文菜单
  46. QMenu *menu = new QMenu(this);
  47. QAction *showAction = menu->addAction("Show Dialog");
  48. QAction *quitAction = menu->addAction("Quit");
  49. // 连接信号和槽
  50. connect(showAction, &QAction::triggered, this, &Dialog::show);
  51. connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);
  52. connect(trayIcon, &QSystemTrayIcon::activated, this, &Dialog::onTrayIconActivated);
  53. // 设置托盘图标的上下文菜单
  54. trayIcon->setContextMenu(menu);
  55. // 显示托盘图标
  56. trayIcon->show();
  57. // 检查系统托盘是否可用
  58. if (!trayIcon->isSystemTrayAvailable()) {
  59. // 如果托盘不可用,则可能需要显示对话框或采取其他措施
  60. QMessageBox::warning(this, "Tray Icon", "System tray is not available.");
  61. show(); // 显示对话框作为备选方案
  62. }
  63. }
  64. // 槽函数:处理托盘图标的激活事件
  65. void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason) {
  66. if (reason == QSystemTrayIcon::Trigger) {
  67. // 当用户点击托盘图标时显示对话框
  68. show();
  69. activateWindow(); // 激活窗口
  70. raise(); // 将窗口置于其他窗口之上
  71. }
  72. }
  73. private: void irdetnetproc();
  74. private:
  75. QTcpSocket* ptcpSocket = nullptr;
  76. QTcpSocket* ptcpSocket1 = nullptr;
  77. private slots:
  78. void on_OpenPortButton_clicked();
  79. void on_OpenButton_clicked();
  80. void on_SavePicButton_clicked();
  81. void on_CloseButton_clicked();
  82. void on_minimizeButton_clicked() {
  83. hide(); // 模拟最小化
  84. }
  85. //void on_pushButton_clicked();
  86. void on_SendCmdButton_clicked();
  87. void grabPicandRes();
  88. //重写定时器的事件 虚函数 子类重写父类的虚函数
  89. virtual void timerEvent(QTimerEvent *);
  90. public:
  91. Ui::Dialog *ui;
  92. int hstopheattimerid; //定时器1的唯一标示
  93. int hkilltimerid; //
  94. int htimersampleid; //采样定时器
  95. QString strnewidr = "";
  96. QString strposition = "";
  97. qint64 qiheatms = 0;
  98. public:
  99. SerailPort* serailPort;
  100. hkcamera* phkcamera = nullptr;
  101. //public slots:
  102. // void slotNewConnection();
  103. // void slotReadyRead();
  104. //private:
  105. // // 负责监听的套接字
  106. // QTcpServer* m_server;
  107. // // 负责通信的套接字
  108. // QTcpSocket* m_client;
  109. };
  110. #endif // DIALOG_H