整合toast

qt6
zcy 2020-12-07 01:05:32 +08:00
parent 0b958aec4e
commit 44177aa13c
4 changed files with 140 additions and 0 deletions

82
Qss.cpp
View File

@ -16,6 +16,12 @@
#include <QDesktopWidget> #include <QDesktopWidget>
#include "windows.h" #include "windows.h"
#include "winuser.h" #include "winuser.h"
#include <QPropertyAnimation>
#include <QScreen>
#include <QGuiApplication>
#include <QPainter>
#include <QTimer>
#define QSSDIALOG_SHADOW_WIDTH 12 //QFrame#dialog,QFrame#messagebox padding #define QSSDIALOG_SHADOW_WIDTH 12 //QFrame#dialog,QFrame#messagebox padding
#define QSSDIALOG_BODER_WIDTH 0 #define QSSDIALOG_BODER_WIDTH 0
@ -268,6 +274,7 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
m_rcNormal = this->parentWidget()->geometry(); m_rcNormal = this->parentWidget()->geometry();
m_rcNormalCentral = this->geometry(); m_rcNormalCentral = this->geometry();
connect(this->titleBar(),SIGNAL( OnMaxOrRestore(bool )),this,SLOT(OnMaxOrRestore(bool))); connect(this->titleBar(),SIGNAL( OnMaxOrRestore(bool )),this,SLOT(OnMaxOrRestore(bool)));
} }
QssMainWindow::~QssMainWindow() QssMainWindow::~QssMainWindow()
@ -1544,3 +1551,78 @@ QssPushButton::QssPushButton(QWidget *parent, QString objName):
this->setObjectName(objName); this->setObjectName(objName);
} }
QssToastWidget::QssToastWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::Tool);// 无边框 无任务栏
setAttribute(Qt::WA_TranslucentBackground, true); // 背景透明
}
QssToastWidget::~QssToastWidget()
{
}
void QssToastWidget::setText(const QString& text)
{
ui.label->setText(text);
}
void QssToastWidget::showAnimation(int timeout /*= 2000*/)
{
// 开始动画
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();
show();
QTimer::singleShot(timeout, [&]
{
// 结束动画
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(1000);
animation->setStartValue(1);
animation->setEndValue(0);
animation->start();
connect(animation, &QPropertyAnimation::finished, [&]
{
close();
deleteLater();// 关闭后析构
});
});
}
void QssToastWidget::showTip(const QString& text, QWidget* parent /*= nullptr*/)
{
QssToastWidget* toast = new QssToastWidget(parent);
toast->setWindowFlags(toast->windowFlags() | Qt::WindowStaysOnTopHint); // 置顶
toast->setText(text);
toast->setStyleSheet("font:bold;font-size:24px;color:rgb(255,255,255);");
toast->adjustSize(); //设置完文本后调整下大小
// 测试显示位于主屏的70%高度位置
QScreen* pScreen = QGuiApplication::primaryScreen();
toast->move((pScreen->size().width() - toast->width()) / 2,
pScreen->size().height() * 5 / 10);
toast->showAnimation(1000);
}
void QssToastWidget::paintEvent(QPaintEvent *event)
{
QPainter paint(this);
paint.begin(this);
auto kBackgroundColor = QColor(255, 255, 255);
kBackgroundColor.setAlpha(0.0 * 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();
}

25
Qss.h
View File

@ -13,6 +13,8 @@
#include <QDebug> #include <QDebug>
#include "windows.h" #include "windows.h"
#include "winuser.h" #include "winuser.h"
#include "ui_qsstoast.h"
class QPushButton; class QPushButton;
class QLabel; class QLabel;
@ -408,6 +410,29 @@ private:
QRect m_rcValid; QRect m_rcValid;
}; };
class QssToastWidget : public QWidget
{
Q_OBJECT
public:
QssToastWidget(QWidget *parent = Q_NULLPTR);
~QssToastWidget();
void setText(const QString& text);
void showAnimation(int timeout = 2000);// 动画方式show出默认2秒后消失
public:
// 静态调用
static void showTip(const QString& text, QWidget* parent = nullptr);
protected:
virtual void paintEvent(QPaintEvent *event);
private:
Ui::Toast ui;
};
#define tipBox(text) QssMessageBox::tips(text) #define tipBox(text) QssMessageBox::tips(text)
#define warnBox(text) QssMessageBox::warn(text) #define warnBox(text) QssMessageBox::warn(text)
#define errBox(text) QssMessageBox::error(text) #define errBox(text) QssMessageBox::error(text)

32
forms/qsstoast.ui Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Toast</class>
<widget class="QWidget" name="Toast">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>932</width>
<height>59</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>170</x>
<y>10</y>
<width>231</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -6,4 +6,5 @@ HEADERS += $$PWD/Qss.h
SOURCES += $$PWD/Qss.cpp SOURCES += $$PWD/Qss.cpp
RESOURCES += $$PWD/qss.qrc RESOURCES += $$PWD/qss.qrc
FORMS += $$PWD/forms/qsstoast.ui