添加一个遮罩层的实现

qt6
zcy 2021-05-05 22:59:44 +08:00
parent f601f3b5cb
commit df324da102
2 changed files with 102 additions and 18 deletions

94
Qss.cpp
View File

@ -21,6 +21,7 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QPainter> #include <QPainter>
#include <QTimer> #include <QTimer>
#include <QCoreApplication>
#define QSSDIALOG_SHADOW_WIDTH 12 #define QSSDIALOG_SHADOW_WIDTH 12
@ -55,15 +56,15 @@ QssTtitleBar::QssTtitleBar(QWidget *parent ,
m_maxBtn->setToolTip(QString::fromLocal8Bit("")); m_maxBtn->setToolTip(QString::fromLocal8Bit(""));
m_maxBtn->setVisible(m_type & QTitleBar_Button_Max); m_maxBtn->setVisible(m_type & QTitleBar_Button_Max);
m_iconBtn = new QPushButton(this); // m_iconBtn = new QPushButton(this);
m_iconBtn->setObjectName("titlebaricon"); // m_iconBtn->setObjectName("titlebaricon");
m_titlebarTitle = new QLabel(this); m_titlebarTitle = new QLabel(this);
m_titlebarTitle->setObjectName("title"); m_titlebarTitle->setObjectName("title");
QHBoxLayout* hBox = new QHBoxLayout(this); QHBoxLayout* hBox = new QHBoxLayout(this);
hBox->setMargin(0); //hBox->setMargin(0);
hBox->addWidget(m_iconBtn); //hBox->addWidget(m_iconBtn);
hBox->addStretch(20); hBox->addStretch(20);
hBox->addWidget(m_titlebarTitle); hBox->addWidget(m_titlebarTitle);
hBox->addStretch(19); hBox->addStretch(19);
@ -81,7 +82,7 @@ QssTtitleBar::QssTtitleBar(QWidget *parent ,
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()));
m_iconBtn->installEventFilter(this);//m_iconLab // m_iconBtn->installEventFilter(this);//m_iconLab
installEventFilter(this); installEventFilter(this);
m_rcValid = QApplication::desktop()->availableGeometry(); m_rcValid = QApplication::desktop()->availableGeometry();
@ -114,7 +115,7 @@ void QssTtitleBar::setTitle( QString title )
void QssTtitleBar::setIcon( QIcon icon) void QssTtitleBar::setIcon( QIcon icon)
{ {
m_iconBtn->setIcon(icon); // m_iconBtn->setIcon(icon);
} }
void QssTtitleBar::setMaxOrRestore( bool val) void QssTtitleBar::setMaxOrRestore( bool val)
@ -250,6 +251,7 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
m_mousePressedInBoundy(false), m_mousePressedInBoundy(false),
m_bLeftPress(false) m_bLeftPress(false)
{ {
QEvent::registerEventType();
m_rcValid = QApplication::desktop()->availableGeometry(); m_rcValid = QApplication::desktop()->availableGeometry();
m_frame = new QFrame(parent, flags); m_frame = new QFrame(parent, flags);
m_frame->setObjectName("window"); m_frame->setObjectName("window");
@ -269,7 +271,7 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
vbox->setMargin(1); vbox->setMargin(1);
vbox->setSpacing(0); vbox->setSpacing(0);
vbox->addWidget(this); vbox->addWidget(this);
installEventFilter(this); // installEventFilter(this);
mFrameRect = m_frame->geometry(); mFrameRect = m_frame->geometry();
m_rcNormal = m_frame->geometry(); m_rcNormal = m_frame->geometry();
@ -291,6 +293,11 @@ QssMainWindow::~QssMainWindow()
m_frame->deleteLater(); m_frame->deleteLater();
} }
QWidget *QssMainWindow::TitleBar()
{
return m_titleBar;
}
void QssMainWindow::show() void QssMainWindow::show()
{ {
m_frame->show(); m_frame->show();
@ -510,7 +517,7 @@ void QssMainWindow::showEvent(QShowEvent *ev)
} }
bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev ){ bool QssMainWindow::eventFilter(QObject * obj, QEvent * ev){
if (obj == m_frame) if (obj == m_frame)
{ {
if (ev->type() == QEvent::MouseMove) if (ev->type() == QEvent::MouseMove)
@ -519,7 +526,7 @@ bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev ){
if (ev) if (ev)
{ {
onMouseMoveEvent(mouseEv); onMouseMoveEvent(mouseEv);
return true; return false;
} }
} }
else if (ev->type() == QEvent::MouseButtonPress) else if (ev->type() == QEvent::MouseButtonPress)
@ -528,7 +535,7 @@ bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev ){
if (ev) if (ev)
{ {
onMousePressEvent(mouseEv); onMousePressEvent(mouseEv);
return true; return false;
} }
} }
else if (ev->type() == QEvent::MouseButtonRelease) else if (ev->type() == QEvent::MouseButtonRelease)
@ -539,12 +546,11 @@ bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev ){
if (ev) if (ev)
{ {
onMouseReleaseEvent(mouseEv); onMouseReleaseEvent(mouseEv);
return true; return false;
} }
} }
else if (ev->type() == QEvent::Show) else if (ev->type() == QEvent::Show)
{ {
/* */
QRect rc = m_frame->rect(); QRect rc = m_frame->rect();
QRect parentRc = m_rcValid; QRect parentRc = m_rcValid;
@ -558,18 +564,24 @@ bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev ){
close(); close();
m_titleBar->close(); m_titleBar->close();
return true; return false;
} }
} }
else if (obj == m_titleBar) else if (obj == m_titleBar)
{ {
qDebug()<<"resize";
QCoreApplication::sendEvent(this,new QEvent(QEvent::Type::Resize));
if (ev->type() == QEvent::Enter) if (ev->type() == QEvent::Enter)
{ {
m_left = false;m_right = false; m_top = false; m_bottom = false; m_left = false;
m_right = false;
m_top = false;
m_bottom = false;
if (m_frame->cursor().shape() != Qt::ArrowCursor) if (m_frame->cursor().shape() != Qt::ArrowCursor)
m_frame->setCursor(Qt::ArrowCursor); m_frame->setCursor(Qt::ArrowCursor);
} }
if(ev->type() == QEvent::Resize){ if(ev->type() == QEvent::Resize){
} }
} }
else if (obj == this) else if (obj == this)
@ -590,7 +602,7 @@ bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev ){
if (ev) if (ev)
{ {
onMouseReleaseEvent(mouseEv); onMouseReleaseEvent(mouseEv);
return true; return false;
} }
} }
if(QEvent::Resize == ev->type()){ if(QEvent::Resize == ev->type()){
@ -601,7 +613,7 @@ bool QssMainWindow::eventFilter( QObject * obj, QEvent * ev ){
QDesktopWidget desktop; QDesktopWidget desktop;
QRect sizeHint = desktop.availableGeometry(-1); QRect sizeHint = desktop.availableGeometry(-1);
} }
return QMainWindow::eventFilter(obj, ev); return false;
} }
@ -623,7 +635,7 @@ bool QssMainWindow::nativeEvent(const QByteArray &eventType, void *message, long
}*/ }*/
} }
} }
return QMainWindow::nativeEvent(eventType, message, result); return false;
} }
void QssMainWindow::ScaleChanged(float scale) void QssMainWindow::ScaleChanged(float scale)
@ -1595,6 +1607,7 @@ void QssToastWidget::showTip(const QString& text, QWidget* parent /*= nullptr*/)
toast->setText(text); toast->setText(text);
toast->setStyleSheet("font:bold;font-size:10px;color:rgb(255,255,255);"); toast->setStyleSheet("font:bold;font-size:10px;color:rgb(255,255,255);");
toast->adjustSize(); toast->adjustSize();
qDebug()<<parent->geometry();
QScreen* pScreen = QGuiApplication::primaryScreen(); QScreen* pScreen = QGuiApplication::primaryScreen();
toast->move((pScreen->size().width() - toast->width()) / 2, toast->move((pScreen->size().width() - toast->width()) / 2,
pScreen->size().height() * 5 / 10); pScreen->size().height() * 5 / 10);
@ -1640,3 +1653,50 @@ void QSSProcessBar::on_done_close()
{ {
this->close(); this->close();
} }
QssMaskWidget::QssMaskWidget(QWidget *parent)
:mParent(nullptr)
{
if(nullptr != parent) {
parent->installEventFilter(this);
mParent = parent;
}
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);// 无边框 无任务栏
setAttribute(Qt::WA_TranslucentBackground, true); // 背景透明
}
QssMaskWidget::~QssMaskWidget()
{
}
bool QssMaskWidget::eventFilter(QObject *obj, QEvent *ev)
{
if(obj != this){
qDebug()<<obj->objectName()<<ev;
if(mParent != nullptr){
if(mParent->parentWidget() != nullptr){
qDebug()<<mParent->parentWidget()->geometry();
this->move(mParent->parentWidget()->geometry().x(),mParent->parentWidget()->geometry().y());
}
}
}
return false;
}
void QssMaskWidget::paintEvent(QPaintEvent *event)
{
QPainter paint(this);
paint.begin(this);
auto kBackgroundColor = QColor(1, 1, 1);
kBackgroundColor.setAlpha(0.1 * 255);// 透明度为0
paint.setRenderHint(QPainter::Antialiasing, true);
paint.setPen(Qt::NoPen);
paint.setBrush(QBrush(kBackgroundColor, Qt::SolidPattern));//设置画刷形式
paint.drawRect(0, 0, width(), height());
paint.end();
}

26
Qss.h
View File

@ -169,7 +169,6 @@ private:
QPushButton* m_maxBtn; QPushButton* m_maxBtn;
QPushButton* m_restoreBtn; QPushButton* m_restoreBtn;
QPushButton* m_minBtn; QPushButton* m_minBtn;
QPushButton* m_iconBtn;
QLabel* m_titlebarTitle; QLabel* m_titlebarTitle;
QMainWindow *m_Main; QMainWindow *m_Main;
@ -212,9 +211,13 @@ class QssMainWindow : public QMainWindow,ICallDPIChanged
public slots: public slots:
void OnMaxOrRestore(bool max); void OnMaxOrRestore(bool max);
public: public:
typedef enum{
EVENT_MOVE = 523,
} EVENT_CUSTOM;
QssMainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0,float scale = 1); QssMainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0,float scale = 1);
virtual ~QssMainWindow(); virtual ~QssMainWindow();
QWidget *TitleBar();
void show(); void show();
void showMinimized(); void showMinimized();
void showMaximized(); void showMaximized();
@ -413,6 +416,26 @@ private:
}; };
class QssMaskWidget : public QWidget
{
Q_OBJECT
public:
QssMaskWidget(QWidget *parent = Q_NULLPTR);
~QssMaskWidget();
virtual bool eventFilter(QObject * obj, QEvent * ev);
public:
protected:
virtual void paintEvent(QPaintEvent *event);
private:
QWidget *mParent;
Ui::Toast ui;
};
class QssToastWidget : public QWidget class QssToastWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
@ -445,6 +468,7 @@ class QSSProcessBar : public QDialog
public: public:
explicit QSSProcessBar(QWidget *parent = nullptr); explicit QSSProcessBar(QWidget *parent = nullptr);
~QSSProcessBar(); ~QSSProcessBar();
public slots: public slots:
void on_percent(int); void on_percent(int);
void on_done_close(); void on_done_close();