QSSMainwindow添加tray icon支持

qt6
zcy 2023-03-12 17:15:45 +08:00
parent c1890fb547
commit 450bcd6f4f
2 changed files with 106 additions and 15 deletions

102
Qss.cpp
View File

@ -50,12 +50,14 @@ static void rangeObjectList(QObject*obj,int indent){
} }
QssTtitleBar::QssTtitleBar(QWidget *parent , QssTtitleBar::QssTtitleBar(QWidget *parent ,
QTitleBar_Type type/* = QTitleBar_Type_Window*/) QTitleBar_Type type,
bool tray)
: QWidget(parent), : QWidget(parent),
m_Main(nullptr), m_Main(nullptr),
m_maxOrRestore(false), m_maxOrRestore(false),
m_pressed(false), m_pressed(false),
m_type(type) m_type(type),
m_tray(tray)
{ {
setObjectName("qssTitleBar"); setObjectName("qssTitleBar");
m_closeBtn = new QPushButton(this); m_closeBtn = new QPushButton(this);
@ -97,12 +99,18 @@ QssTtitleBar::QssTtitleBar(QWidget *parent ,
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); 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_minBtn, SIGNAL(clicked()), parent, SLOT(showMinimized()));
connect(m_maxBtn, SIGNAL(clicked()), this, SLOT(onMaxOrRestore())); connect(m_maxBtn, SIGNAL(clicked()), this, SLOT(onMaxOrRestore()));
connect(m_restoreBtn, SIGNAL(clicked()), this, SLOT(onMaxOrRestore())); connect(m_restoreBtn, SIGNAL(clicked()), this, SLOT(onMaxOrRestore()));
installEventFilter(this);
m_rcValid = QApplication::desktop()->availableGeometry(); m_rcValid = QApplication::desktop()->availableGeometry();
@ -284,7 +292,19 @@ void QssMainWindow::LogicalDpiChange(qreal dpi)
// auto parent = this->parentWidget(); // auto parent = this->parentWidget();
// parent->setGeometry(parent->geometry().x(),parent->geometry().y(), // parent->setGeometry(parent->geometry().x(),parent->geometry().y(),
// parent->geometry().width()*m_dpi_ratio, // 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), : QMainWindow(parent, flags),
m_mousePressedInBoundy(false), m_mousePressedInBoundy(false),
m_bLeftPress(false), m_bLeftPress(false),
m_dpi_ratio(1.0) m_dpi_ratio(1.0),
m_tray_on(false)
{ {
QEvent::registerEventType(); QEvent::registerEventType();
m_rcValid = QApplication::desktop()->availableGeometry(); m_rcValid = QApplication::desktop()->availableGeometry();
@ -344,7 +365,6 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
SIGNAL(logicalDotsPerInchChanged(qreal)), SIGNAL(logicalDotsPerInchChanged(qreal)),
this,SLOT(LogicalDpiChange(qreal))); this,SLOT(LogicalDpiChange(qreal)));
} }
// rangeObjectList(m_frame,0);
detectDpi(); detectDpi();
return; return;
} }
@ -476,6 +496,13 @@ void QssMainWindow::showNormal()
m_titleBar->setMaxOrRestore(false); m_titleBar->setMaxOrRestore(false);
m_frame->resize(rect().width(), rect().height() + m_titleBar->rect().height()); m_frame->resize(rect().width(), rect().height() + m_titleBar->rect().height());
m_frame->showNormal(); m_frame->showNormal();
m_frame->show();
qDebug()<<m_frame->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 ) void QssMainWindow::setWindowTitle( QString title )
@ -734,6 +761,9 @@ void QssMainWindow::showEvent(QShowEvent *ev)
} }
bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){ bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){
if (ev->type() == QEvent::Hide){
qDebug()<<"hide";
}
if (obj == m_frame) if (obj == m_frame)
{ {
if (ev->type() == QEvent::Paint){ if (ev->type() == QEvent::Paint){
@ -780,10 +810,25 @@ bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){
} }
else if (ev->type() == QEvent::Close) else if (ev->type() == QEvent::Close)
{ {
close(); if(m_tray_on){
m_titleBar->close(); // this->hide();
return true;
return false; }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) 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::Resize){
} }
if(ev->type() == QEvent::Close){
qDebug()<<"close";
return true;
}
} }
else if (obj == this) else if (obj == this)
{ {
@ -825,13 +874,44 @@ bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){
if(QEvent::Resize == ev->type()){ if(QEvent::Resize == ev->type()){
} }
if(QEvent::Close == ev->type()){
qDebug()<<"close";
}
} }
if(QEvent::Resize == ev->type()){ if(QEvent::Resize == ev->type()){
QDesktopWidget desktop; QDesktopWidget desktop;
} }
if(QEvent::Close == ev->type()){
qDebug()<<"close";
return true;
}
return false; 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) void QssMainWindow::ScaleChanged(float scale)

19
Qss.h
View File

@ -12,6 +12,7 @@
#include <QAbstractNativeEventFilter> #include <QAbstractNativeEventFilter>
#include <QDebug> #include <QDebug>
#include <QThread> #include <QThread>
#include <QSystemTrayIcon>
#include "ui_qsstoast.h" #include "ui_qsstoast.h"
#include "ui_process.h" #include "ui_process.h"
@ -66,7 +67,9 @@ public:
QTitleBar_Type_MessageBox = QTitleBar_Button_Close 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(); ~QssTtitleBar();
void setTitle(QString title); void setTitle(QString title);
@ -102,9 +105,10 @@ private:
QRect m_rcValid;//桌面最大可用尺寸 QRect m_rcValid;//桌面最大可用尺寸
QRect m_rcNormal;//还原后窗口尺寸 QRect m_rcNormal;//还原后窗口尺寸
bool m_tray;
QTitleBar_Type m_type; QTitleBar_Type m_type;
}; };
class QssEventFilter class QssEventFilter
: public QAbstractNativeEventFilter : public QAbstractNativeEventFilter
{ {
@ -123,7 +127,8 @@ public slots:
void OnMaxOrRestore(bool max); void OnMaxOrRestore(bool max);
void PhisycalDpiChange(qreal); void PhisycalDpiChange(qreal);
void LogicalDpiChange(qreal); void LogicalDpiChange(qreal);
void TrayIconAction(QSystemTrayIcon::ActivationReason reason);
void restory();
public: public:
typedef enum{ typedef enum{
EVENT_MOVE = 523, EVENT_MOVE = 523,
@ -150,7 +155,7 @@ public:
inline QFrame* frame(){return m_frame;} inline QFrame* frame(){return m_frame;}
int CalCursorCol(QPoint pt); //计算鼠标X的位置 int CalCursorCol(QPoint pt); //计算鼠标X的位置
virtual bool eventFilter(QObject * obj, QEvent * ev); virtual bool eventFilter(QObject * obj, QEvent * ev);
void SetTrayIcon(QIcon *icon);
private: private:
void onMouseMoveEvent(QMouseEvent * ev); void onMouseMoveEvent(QMouseEvent * ev);
void onMousePressEvent(QMouseEvent * ev); void onMousePressEvent(QMouseEvent * ev);
@ -187,7 +192,13 @@ private:
float m_dpi_ratio; float m_dpi_ratio;
QWidget *mShadowMask; QWidget *mShadowMask;
QScreen *mCurrentScreen; 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 class QssDialog : public QDialog