这个是elemnt-ui的原先样式
这个是qt实现的基本样式
核心代码如下:
头文件
#ifndef DTPOPUPWINDOW_H
#define DTPOPUPWINDOW_H
#include <QWidget>
#include <QTimer>
namespace Ui {
class DTPopupWindow;
}
enum E_POPUP_TYPE
{
E_WARNING = 0, // 警告弹窗
E_ERROR, // 错误
E_INFO, // 消息
E_SUCCESS, // 成功
};
class DTPopupWindow : public QWidget
{
Q_OBJECT
public:
explicit DTPopupWindow(QWidget *parent = nullptr);
~DTPopupWindow();
/******设置弹窗类型**********/
void setPopupType(const E_POPUP_TYPE& popupType)
{
m_popupType = popupType;
}
void setPopupText(const QString&);
void popupShow();
private slots:
void slotTimer();
private:
Ui::DTPopupWindow *ui;
E_POPUP_TYPE m_popupType = E_POPUP_TYPE::E_INFO;
QTimer* m_timer ;
};
#endif // DTPOPUPWINDOW_H
实现部分:cpp
#include "dtpopupwindow.h"
#include "ui_dtpopupwindow.h"
#include <QColor>
#include <QDesktopWidget>
#define SUCCESS_BACKGROUND_COLOR QString("240, 249, 235")
#define WARNING_BACKGROUND_COLOR QString("253, 246, 236")
#define INFO_BACKGROUND_COLOR QString("244, 244, 245")
#define ERROR_BACKGROUND_COLOR QString("254, 240, 240")
#define SUCCESS_FONT_COLOR QString("103, 194, 58")
#define WARNING_FONT_COLOR QString("230, 162, 60")
#define INFO_FONT_COLOR QString("205, 173, 153")
#define ERROR_FONT_COLOR QString("247, 108, 108")
DTPopupWindow::DTPopupWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::DTPopupWindow)
{
setWindowFlags(Qt::WindowStaysOnTopHint); // 置顶
setWindowFlag(Qt::FramelessWindowHint);
// setAttribute(Qt::WA_StyledBackground, true);
// setStyleSheet("background-color: rgb(61, 60, 64); font: 12px; color: #EAEAEA;");
ui->setupUi(this);
m_timer = new QTimer(this);
// timer->setSingleShot(true);//仅触发一次
connect(m_timer, SIGNAL(timeout()), this, SLOT(slotTimer()));
}
DTPopupWindow::~DTPopupWindow()
{
delete ui;
}
void DTPopupWindow::setPopupText(const QString & qsText)
{
ui->lbl_tip->setText(qsText);
}
void DTPopupWindow::slotTimer()
{
m_timer->stop();
this->hide();
}
void DTPopupWindow::popupShow()
{
QDesktopWidget *deskTop=QApplication::desktop();
auto deskRect=deskTop->availableGeometry();
QPoint normalPoint;
normalPoint.setX(deskRect.width()/2 - this->width() /2);
normalPoint.setY(20);
this->setGeometry(normalPoint.x(),normalPoint.y(),this->width(),this->height());
QString base_color = ("QWidget { background-color: rgb(%1); font: 14px; color: rgb(%2) }");
switch (m_popupType) {
case E_POPUP_TYPE::E_SUCCESS:
{
QString qsBB = base_color.arg(SUCCESS_BACKGROUND_COLOR).arg(SUCCESS_FONT_COLOR);
this->setStyleSheet(base_color.arg(SUCCESS_BACKGROUND_COLOR).arg(SUCCESS_FONT_COLOR));
break;
}
case E_POPUP_TYPE::E_WARNING:
{
this->setStyleSheet(base_color.arg(WARNING_BACKGROUND_COLOR).arg(WARNING_FONT_COLOR));
break;
}
case E_POPUP_TYPE::E_INFO:
{
this->setStyleSheet(base_color.arg(INFO_BACKGROUND_COLOR).arg(INFO_FONT_COLOR));
break;
}
case E_POPUP_TYPE::E_ERROR:
{
this->setStyleSheet(base_color.arg(ERROR_BACKGROUND_COLOR).arg(ERROR_FONT_COLOR));
break;
}
default:
break;
}
this->show();
m_timer->start(6000); // 持续6秒
}
ui文件部分
/********************************************************************************
** Form generated from reading UI file 'dtpopupwindow.ui'
**
** Created by: Qt User Interface Compiler version 5.14.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_DTPOPUPWINDOW_H
#define UI_DTPOPUPWINDOW_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_DTPopupWindow
{
public:
QHBoxLayout *horizontalLayout;
QSpacerItem *horizontalSpacer;
QLabel *lbl_tip;
QSpacerItem *horizontalSpacer_2;
void setupUi(QWidget *DTPopupWindow)
{
if (DTPopupWindow->objectName().isEmpty())
DTPopupWindow->setObjectName(QString::fromUtf8("DTPopupWindow"));
DTPopupWindow->resize(493, 60);
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(DTPopupWindow->sizePolicy().hasHeightForWidth());
DTPopupWindow->setSizePolicy(sizePolicy);
DTPopupWindow->setMinimumSize(QSize(0, 60));
DTPopupWindow->setMaximumSize(QSize(16777215, 60));
horizontalLayout = new QHBoxLayout(DTPopupWindow);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
horizontalSpacer = new QSpacerItem(193, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer);
lbl_tip = new QLabel(DTPopupWindow);
lbl_tip->setObjectName(QString::fromUtf8("lbl_tip"));
horizontalLayout->addWidget(lbl_tip);
horizontalSpacer_2 = new QSpacerItem(192, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer_2);
retranslateUi(DTPopupWindow);
QMetaObject::connectSlotsByName(DTPopupWindow);
} // setupUi
void retranslateUi(QWidget *DTPopupWindow)
{
DTPopupWindow->setWindowTitle(QCoreApplication::translate("DTPopupWindow", "Form", nullptr));
lbl_tip->setText(QCoreApplication::translate("DTPopupWindow", "TextLabel", nullptr));
} // retranslateUi
};
namespace Ui {
class DTPopupWindow: public Ui_DTPopupWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_DTPOPUPWINDOW_H
使用方式:
DTPopupWindow popWindow;
popWindow.setPopupType(popType);
popWindow.setPopupText(qsInfo);
popWindow.popupShow();
目前该方法已经支持了警告、成功、提示和失败四种样式