From 450bcd6f4fe02a0b7ed670650a698448930c7883 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sun, 12 Mar 2023 17:15:45 +0800 Subject: [PATCH] =?UTF-8?q?QSSMainwindow=E6=B7=BB=E5=8A=A0tray=20icon?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Qss.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++------ Qss.h | 19 ++++++++--- 2 files changed, 106 insertions(+), 15 deletions(-) diff --git a/Qss.cpp b/Qss.cpp index 69366b4..a6b6af4 100644 --- a/Qss.cpp +++ b/Qss.cpp @@ -50,12 +50,14 @@ static void rangeObjectList(QObject*obj,int indent){ } QssTtitleBar::QssTtitleBar(QWidget *parent , - QTitleBar_Type type/* = QTitleBar_Type_Window*/) + QTitleBar_Type type, + bool tray) : QWidget(parent), m_Main(nullptr), m_maxOrRestore(false), m_pressed(false), - m_type(type) + m_type(type), + m_tray(tray) { setObjectName("qssTitleBar"); m_closeBtn = new QPushButton(this); @@ -97,12 +99,18 @@ QssTtitleBar::QssTtitleBar(QWidget *parent , setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - connect(m_closeBtn, SIGNAL(clicked()), parent, SLOT(close())); + installEventFilter(this); + + + if(m_tray){ + connect(m_closeBtn, SIGNAL(clicked()), parent, SLOT(hide())); + }else{ + connect(m_closeBtn, SIGNAL(clicked()), parent, SLOT(close())); + } connect(m_minBtn, SIGNAL(clicked()), parent, SLOT(showMinimized())); connect(m_maxBtn, SIGNAL(clicked()), this, SLOT(onMaxOrRestore())); connect(m_restoreBtn, SIGNAL(clicked()), this, SLOT(onMaxOrRestore())); - installEventFilter(this); m_rcValid = QApplication::desktop()->availableGeometry(); @@ -284,7 +292,19 @@ void QssMainWindow::LogicalDpiChange(qreal dpi) // auto parent = this->parentWidget(); // parent->setGeometry(parent->geometry().x(),parent->geometry().y(), // parent->geometry().width()*m_dpi_ratio, -// parent->geometry().height()*m_dpi_ratio); + // parent->geometry().height()*m_dpi_ratio); +} + +void QssMainWindow::TrayIconAction(QSystemTrayIcon::ActivationReason reason) +{ + if(reason == QSystemTrayIcon::Trigger) + this->showNormal(); +} + +void QssMainWindow::restory() +{ + this->showNormal(); + } @@ -292,7 +312,8 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* = : QMainWindow(parent, flags), m_mousePressedInBoundy(false), m_bLeftPress(false), - m_dpi_ratio(1.0) + m_dpi_ratio(1.0), + m_tray_on(false) { QEvent::registerEventType(); m_rcValid = QApplication::desktop()->availableGeometry(); @@ -344,7 +365,6 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* = SIGNAL(logicalDotsPerInchChanged(qreal)), this,SLOT(LogicalDpiChange(qreal))); } -// rangeObjectList(m_frame,0); detectDpi(); return; } @@ -476,6 +496,13 @@ void QssMainWindow::showNormal() m_titleBar->setMaxOrRestore(false); m_frame->resize(rect().width(), rect().height() + m_titleBar->rect().height()); m_frame->showNormal(); + m_frame->show(); + qDebug()<children(); + for (int i = 0;i < m_frame->children().size();i++){ + if(m_frame->children().at(i)->isWidgetType()){ + ((QWidget*)m_frame->children().at(i))->show(); + } + } } void QssMainWindow::setWindowTitle( QString title ) @@ -734,6 +761,9 @@ void QssMainWindow::showEvent(QShowEvent *ev) } bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){ + if (ev->type() == QEvent::Hide){ + qDebug()<<"hide"; + } if (obj == m_frame) { if (ev->type() == QEvent::Paint){ @@ -780,10 +810,25 @@ bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){ } else if (ev->type() == QEvent::Close) { - close(); - m_titleBar->close(); - - return false; + if(m_tray_on){ +// this->hide(); + return true; + }else{ + close(); + m_titleBar->close(); + return true; + } + } + else if (ev->type() == QEvent::Hide) + { + if(m_tray_on){ + this->hide(); + return true; + }else{ + close(); + m_titleBar->close(); + return true; + } } } else if (obj == m_titleBar) @@ -800,6 +845,10 @@ bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){ if(ev->type() == QEvent::Resize){ } + if(ev->type() == QEvent::Close){ + qDebug()<<"close"; + return true; + } } else if (obj == this) { @@ -825,13 +874,44 @@ bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){ if(QEvent::Resize == ev->type()){ } + if(QEvent::Close == ev->type()){ + qDebug()<<"close"; + } } if(QEvent::Resize == ev->type()){ QDesktopWidget desktop; } + if(QEvent::Close == ev->type()){ + qDebug()<<"close"; + return true; + } return false; } +void QssMainWindow::SetTrayIcon(QIcon *icon) +{ +// QPixmap m_logo(":/gif.ico"); + +// m_tray->setIcon(QIcon(m_logo));//设置图标 +// m_tray->show(); +// connect(m_tray,&QSystemTrayIcon::activated, +// this,&QssMainWindow::TrayIconAction); +// m_menu = new QMenu(this); +// m_resetAction = new QAction(this); +// m_resetAction->setText("show"); +// m_quitAction = new QAction(this); +// m_resetAction->setIcon(QIcon(m_logo)); +// m_quitAction->setText("quit"); +// m_quitAction->setIcon(QIcon(m_logo)); +// connect(m_quitAction,&QAction::triggered,qApp,&QApplication::quit); +//// connect(m_resetAction,&QAction::triggered,this,&MainWindow::restory); + +// m_tray->setContextMenu(m_menu);//设置托盘菜单 +// m_menu->addAction(m_resetAction); +// m_menu->addAction(m_quitAction); + m_tray_on = true; +} + void QssMainWindow::ScaleChanged(float scale) diff --git a/Qss.h b/Qss.h index 4ce0e42..8c6f04f 100644 --- a/Qss.h +++ b/Qss.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "ui_qsstoast.h" #include "ui_process.h" @@ -66,7 +67,9 @@ public: QTitleBar_Type_MessageBox = QTitleBar_Button_Close }; - QssTtitleBar(QWidget *parent, QTitleBar_Type type = QTitleBar_Type_MainWindow); + QssTtitleBar(QWidget *parent, + QTitleBar_Type type = QTitleBar_Type_MainWindow, + bool tray = true); ~QssTtitleBar(); void setTitle(QString title); @@ -102,9 +105,10 @@ private: QRect m_rcValid;//桌面最大可用尺寸 QRect m_rcNormal;//还原后窗口尺寸 - + bool m_tray; QTitleBar_Type m_type; }; + class QssEventFilter : public QAbstractNativeEventFilter { @@ -123,7 +127,8 @@ public slots: void OnMaxOrRestore(bool max); void PhisycalDpiChange(qreal); void LogicalDpiChange(qreal); - + void TrayIconAction(QSystemTrayIcon::ActivationReason reason); + void restory(); public: typedef enum{ EVENT_MOVE = 523, @@ -150,7 +155,7 @@ public: inline QFrame* frame(){return m_frame;} int CalCursorCol(QPoint pt); //计算鼠标X的位置 virtual bool eventFilter(QObject * obj, QEvent * ev); - + void SetTrayIcon(QIcon *icon); private: void onMouseMoveEvent(QMouseEvent * ev); void onMousePressEvent(QMouseEvent * ev); @@ -187,7 +192,13 @@ private: float m_dpi_ratio; QWidget *mShadowMask; QScreen *mCurrentScreen; + bool m_tray_on; + QSystemTrayIcon* m_tray; //托盘类 + QMenu *m_menu; //托盘菜单 + QAction *m_resetAction; //托盘按钮 + QAction *m_quitAction; //托盘按钮 + QPixmap *m_logo; // 贴图 }; class QssDialog : public QDialog