自定义弹窗dialog
MDialog.h
#pragma once
#include <QDialog>
//#include "ui_MDialog.h"
#include <QGroupBox>
#include <QListWidget>
#include <QHBoxLayout>
#include <QWidget>
#include <QRadioButton>
#include <QLabel>
#include <QListView>
#include <QTableWidget>
#include <QPushButton>
#include <QFile>
#include <QMouseEvent>
#include <QPainter>
#include <qmath.h>
#include <QDebug>
#include <QButtonGroup>
#include <QApplication>
#include<windows.h>
class MDialog : public QDialog
{
Q_OBJECT
public:
MDialog(QWidget *parent = Q_NULLPTR);
~MDialog();
public:
int getResult(); //提供给外部用户点击的结果
bool getForceQuitResult();
void setSecureTipWidgetShow(bool bChoose);
void ShowSureQuit();
void setQuitLabel(QString str);//设置窗口内容
void setDialogTitle(QString str); //设置弹窗的标题
void init();
void mQuit();
bool mIsShow();
void setDlgShowSymbol(bool bIsShow);
private:
QWidget* createListItemWidget(QString strLabelTitlr, QString strLabelContent); //创建listwidgetItem成员
void setButtonStyleSheet(QPushButton*& pBtn);//设置按钮样式
void setListStyleSheet(QListWidget*& listWidget);//设置ListWidget样式
void setResult(int nResult);//记录用户选择的结果
void setCursorType(int flag);
public slots:
void onTitleCloseBtnClicked(bool);
void onForceCloseBtnClicked(bool);
void onListWidgetCurrentItemChanged(QListWidgetItem* pCurrent, QListWidgetItem* pPrevious);
void onSureQuitBtnClicked(bool);
void onButtonGroupClicked(int nClicked);
protected:
bool eventFilter(QObject *obj, QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
int countFlag(QPoint p, int row);
int countRow(QPoint p);
//界面重绘响应
virtual void paintEvent(QPaintEvent *event);
private:
//Ui::MDialog ui;
QVBoxLayout* m_pMainVLayout; //dialog主布局
QListWidget* m_pListWidget; //选项列表
QHBoxLayout* m_pListWidgetLayout;
QHBoxLayout* m_pButtonHLayout;
QHBoxLayout* m_pForceCloseAndCancelLayout;
QHBoxLayout* m_pQuitContentLayout; //提示内容布局
QLabel *m_pQuitLabel; //强制关闭提示信息
QPushButton* m_pForceCloseBtn; //强制关闭按钮
QPushButton* m_pCancelBtn; //取消按钮
QPushButton* m_pTitleCloseBtn; //关闭窗口按钮
QLabel* m_pMainTitleLabel; //标题
QPushButton* m_pSureQuitBtn;//确认退出
QLabel* m_pToolTipPicture; //提示图标
QButtonGroup* m_pButtonGroup;
int m_nButtonIndex;
int m_nRaidoBtnChoose; //即将退出账号的退出选择
bool m_bNeedShowSecureTip; //是否需要弹出安全提示退出界面
bool m_bIsClickedForceQuitBtn; //是否按下强制退出按钮
double m_dpi;
bool m_bIsExec; //是否已经弹窗标识
//拖动变量
bool bKeyMovePressed_; //拖拽窗口标记
QPoint ptStartPos_;
};
class SecureTipDialog : public QDialog
{
Q_OBJECT
public:
SecureTipDialog(QWidget *parent = Q_NULLPTR);
~SecureTipDialog();
public:
QListWidget* m_pListWidget; //选项列表
QPushButton* m_pSureCancelBtn;//取消
public:
int getResult(); //提供给外部用户点击的结果
void setDpi(double fDpi);
void init();
private:
QWidget* createListItemWidget(QString strLabelTitlr, QString strLabelContent); //创建listwidgetItem成员
void setButtonStyleSheet(QPushButton*& pBtn);//设置按钮样式
void setListStyleSheet(QListWidget*& listWidget);//设置ListWidget样式
void setResult(int nResult);//记录用户选择的结果
public slots:
void onTitleCloseBtnClicked(bool);
void onListWidgetCurrentItemChanged(QListWidgetItem* pCurrent, QListWidgetItem* pPrevious);
void onSureQuitBtnClicked(bool);
void onButtonGroupClicked(int nClicked);
protected:
bool eventFilter(QObject *obj, QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
int countFlag(QPoint p, int row);
int countRow(QPoint p);
//界面重绘响应
virtual void paintEvent(QPaintEvent *event);
private:
//Ui::MDialog ui;
QVBoxLayout* m_pMainVLayout; //dialog主布局
QHBoxLayout* m_pListWidgetLayout;
QHBoxLayout* m_pButtonHLayout;
QPushButton* m_pTitleCloseBtn; //关闭窗口按钮
QLabel* m_pMainTitleLabel; //标题
QPushButton* m_pSureQuitBtn;//确认退出
QHBoxLayout* m_pQuitContentLayout;//提示label布局
QButtonGroup* m_pButtonGroup;
int m_nButtonIndex;
//标题拖动、双击最大化
QPoint mDragPos;
bool _isleftpressed;
int _curpos;
QPoint _plast;
int m_nRaidoBtnChoose; //即将退出账号的退出选择
double m_dDpi;
};
class MessageDialog : public QDialog
{
Q_OBJECT
public:
MessageDialog(QWidget *parent = Q_NULLPTR);
~MessageDialog();
public:
int getResult();
void setTitleText(QString strTitle);
void setContentText(QString strContent);
void setBtnOneText(QString strBtn1text);
void setBtnTwoText(QString strBtn2text);
private:
void setButtonStyleSheet(QPushButton*& pBtn, bool bBlue);
void setResult(int nResult);
public slots:
void onTitleCloseBtnClicked(bool);
void onForceCloseBtnClicked(bool);
protected:
bool eventFilter(QObject *obj, QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
int countFlag(QPoint p, int row);
int countRow(QPoint p);
//界面重绘响应
virtual void paintEvent(QPaintEvent *event);
private:
QVBoxLayout* m_pMainVLayout; //dialog主布局
QHBoxLayout* m_pForceCloseAndCancelLayout;
QLabel *m_pQuitLabel; //强制关闭提示信息
QPushButton* m_pForceCloseBtn; //强制关闭按钮
QPushButton* m_pCancelBtn; //取消按钮
QPushButton* m_pTitleCloseBtn; //关闭窗口按钮
QLabel* m_pMainTitleLabel; //标题
//标题拖动、双击最大化
QPoint mDragPos;
bool _isleftpressed;
int _curpos;
QPoint _plast;
int m_nResult;
double m_dDpi;
};
MDialog.cpp
#include "MDialog.h"
#define MARGIN 5 //四个角的长度
#define SHADOW_WIDTH 10 //边框阴影宽度
MDialog::MDialog(QWidget *parent)
: QDialog(parent)
{
m_bIsExec = true;
bKeyMovePressed_ = false;
m_nRaidoBtnChoose = -1;
m_nButtonIndex = 0;
m_bNeedShowSecureTip = true;
m_bIsClickedForceQuitBtn = false;
m_dpi = 1;
m_pMainVLayout = new QVBoxLayout(this);
QHBoxLayout* pTitleHLayout = new QHBoxLayout(this);
m_pQuitContentLayout = new QHBoxLayout(this);
m_pMainTitleLabel = new QLabel(this);
m_pMainTitleLabel->setText(QString::fromLocal8Bit("退出账号"));
this->setMouseTracking(true);
Qt::WindowFlags flags = Qt::Dialog;
flags |= Qt::FramelessWindowHint;
flags |= Qt::Tool; //程序不在任务栏显示
flags |= Qt::WindowStaysOnTopHint; //置顶显示
setWindowFlags(flags);
setAttribute(Qt::WA_TranslucentBackground);
//this->setStyleSheet("QDialog{border-radius:15px;}");
QFont font(QString::fromLocal8Bit("微软雅黑"), 13, 6);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75)
m_pMainTitleLabel->setFont(font);
QPalette pa;
pa.setColor(QPalette::WindowText, Qt::black);
m_pMainTitleLabel->setPalette(pa);
m_pTitleCloseBtn = new QPushButton(this);
m_pTitleCloseBtn->installEventFilter(this);
m_pTitleCloseBtn->setMaximumWidth(12 * m_dpi);
m_pTitleCloseBtn->setMaximumHeight(12 * m_dpi);
m_pTitleCloseBtn->setIconSize(QSize(12 * m_dpi, 12 * m_dpi));
m_pTitleCloseBtn->setToolTip(QString::fromLocal8Bit("关闭"));
/*m_pTitleCloseBtn->setStyleSheet("QPushButton{ border-image:url('../dialog1/p2.png');}QPushButton:hover{ border-image:url('../dialog1/p2.png');\
background-color: qlineargradient(spread: pad, x1: 0, y1: 1, x2: 0, y2: 0, stop: 0 rgba(238, 0, 0, 128), stop: 1 rgba(238, 44, 44, 255));\
}QPushButton{border-radius:0px;color: #F0F0F0;background-color:rgba(0, 0, 0, 0);border-style:none;}");*/
m_pTitleCloseBtn->setStyleSheet("QPushButton{ border-image:url('../dialog1/p2.png');}");
QVBoxLayout* pTilteLayout = new QVBoxLayout(this);
pTitleHLayout->addWidget(m_pMainTitleLabel);
pTitleHLayout->addWidget(m_pTitleCloseBtn);
pTitleHLayout->setMargin(1);
pTilteLayout->addLayout(pTitleHLayout, 1);
QLabel *blueLine = new QLabel(this);
blueLine->setMaximumHeight(1);
blueLine->setStyleSheet("background-color:rgb(227, 227, 227);");
pTilteLayout->addWidget(blueLine);
pTilteLayout->setMargin(1);
m_pMainVLayout->addLayout(pTilteLayout);
m_pQuitLabel = new QLabel(this);
m_pQuitLabel->setWordWrap(true);
QString text;
text.append("<p style='line-height:20px'>").append(QString::fromLocal8Bit("此操作将强制关闭当前打开的文档,可能会"))
.append("\n\n").append("<p style='line-height:20px'>").append(QString::fromLocal8Bit("造成数据丢失, 是否继续 ?")).append("</p>");
m_pQuitLabel->setText(text);
QFont fontQuitLabel(QString::fromLocal8Bit("微软雅黑"), 11, 3);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75)
m_pQuitLabel->setFont(fontQuitLabel);
QPalette paQuitLabel;
paQuitLabel.setColor(QPalette::Text, QColor(66, 66, 66, 255));
m_pQuitLabel->setPalette(paQuitLabel);
//添加图片
m_pToolTipPicture = new QLabel(this);
m_pToolTipPicture->setMinimumSize(QSize(30 * m_dpi, 30 * m_dpi));
QPixmap pixmapTask("../dialog1/p1.png");
m_pToolTipPicture->setPixmap(pixmapTask);
m_pToolTipPicture->setAlignment(Qt::AlignCenter);
m_pQuitContentLayout->addWidget(m_pToolTipPicture, 1);
m_pQuitContentLayout->addWidget(m_pQuitLabel, 3);
m_pMainVLayout->addLayout(m_pQuitContentLayout, 5);
m_pForceCloseBtn = new QPushButton(this);
m_pForceCloseBtn->setText(QString::fromLocal8Bit("是"));
m_pForceCloseBtn->setMinimumSize(QSize(100 * m_dpi, 30 * m_dpi));
//m_pForceCloseBtn->setChecked(true);
m_pCancelBtn = new QPushButton(this);
m_pCancelBtn->setText(QString::fromLocal8Bit("否"));
m_pCancelBtn->setChecked(true);
m_pCancelBtn->setMinimumSize(QSize(100 * m_dpi, 30 * m_dpi));
m_pForceCloseAndCancelLayout = new QHBoxLayout(this);
setButtonStyleSheet(m_pForceCloseBtn);
setButtonStyleSheet(m_pCancelBtn);
m_pForceCloseAndCancelLayout->addStretch(6);
m_pForceCloseAndCancelLayout->addWidget(m_pForceCloseBtn);
m_pForceCloseAndCancelLayout->addWidget(m_pCancelBtn);
m_pForceCloseAndCancelLayout->addStretch(1);
m_pMainVLayout->addLayout(m_pForceCloseAndCancelLayout, 1);
m_pMainVLayout->addStretch(1);
this->setMinimumSize(QSize(430 * m_dpi, 180 * m_dpi));
this->setStyleSheet("background-color: white");
m_pMainVLayout->setMargin(SHADOW_WIDTH + 8);
this->setLayout(m_pMainVLayout);
connect(m_pTitleCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
connect(m_pForceCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onForceCloseBtnClicked(bool)));
connect(m_pCancelBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
}
MDialog::~MDialog()
{
qDebug() <<QString::fromLocal8Bit("析构函数,用户选择了:") << getResult();
}
int MDialog::getResult()
{
setDlgShowSymbol(false);
return m_nRaidoBtnChoose;
}
bool MDialog::getForceQuitResult()
{
setDlgShowSymbol(false);
return m_bIsClickedForceQuitBtn;
}
void MDialog::setSecureTipWidgetShow(bool bChoose)
{
m_bNeedShowSecureTip = bChoose;
}
//************************************
// Method: ShowSureQuit
// Description: 只显示安全提示页面
// Author: xxxx
// Date: 2020/10/26
// Returns: void
//************************************
void MDialog::ShowSureQuit()
{
m_pForceCloseBtn->clicked(true);
}
void MDialog::setQuitLabel(QString str)
{
m_pQuitLabel->setText(str);
}
void MDialog::setDialogTitle(QString str)
{
m_pMainTitleLabel->setText(str);
}
void MDialog::init()
{
m_nRaidoBtnChoose = -1;
setDlgShowSymbol(true);
}
void MDialog::mQuit()
{
m_nRaidoBtnChoose = -1;
this->close();
}
bool MDialog::mIsShow()
{
return m_bIsExec;
}
//************************************
// Method: createListItemWidget
// Description: 整个函数是为了组合成listWidget的一行
// Author: xxx
// Date:2020/10/26
// Returns: ListWidget的每行内容
// Parameter: QString strLabelTitle:窗口标题
// Parameter: QString strLabelContent:窗口内容
//************************************
QWidget* MDialog::createListItemWidget(QString strLabelTitle, QString strLabelContent)
{
QWidget* container = new QWidget(this);
QHBoxLayout* pContainerLayout = new QHBoxLayout(container);
QVBoxLayout* pLabelLayout = new QVBoxLayout(container);
QHBoxLayout* pButtonLayout = new QHBoxLayout(container);
QRadioButton* pRBChoose = new QRadioButton(container);
m_pButtonGroup->addButton(pRBChoose, m_nButtonIndex);
pRBChoose->setIconSize(QSize(30, 30));
QLabel* pLabelTitle = new QLabel(container);
pLabelTitle->setText(strLabelTitle);
QFont ft(QString::fromLocal8Bit("微软雅黑"), 11, 3);
QPalette pa;
pa.setColor(QPalette::WindowText, QColor(66, 66, 66, 255));
pLabelTitle->setPalette(pa);
pLabelTitle->setFont(ft);
pLabelLayout->addWidget(pLabelTitle);
pButtonLayout->addWidget(pRBChoose);
pContainerLayout->addLayout(pButtonLayout, 1);
pContainerLayout->addLayout(pLabelLayout, 5);
container->setLayout(pContainerLayout);
m_nButtonIndex++;
return container;
}
//************************************
// Method: setButtonStyleSheet
// Description:按钮的样式
// Author:xxx
// Date:2020/10/26
// Returns: void
// Parameter: QPushButton * & pBtn
//************************************
void MDialog::setButtonStyleSheet(QPushButton*& pBtn)
{
QString strStyle = "";
if (pBtn->text() == QString::fromLocal8Bit("否") || pBtn->text() == QString::fromLocal8Bit("确认退出"))
{
strStyle = "QPushButton{/*font-family:Microsoft Yahei;*//*字体大小为20点*/font-size:12pt;/*字体颜色为白色*/color:white;/*背景颜色*/background-color:rgb(65, 127, 249);/*边框圆角半径为5像素*/border-radius:5px;height:30;/* 边框风格 */border-style:outset;/* 边框宽度 */border-width:1px;/* 边框颜色 */border-color:rgb(216, 216, 216);}";
}
else
{
strStyle = "QPushButton{/*font-family:Microsoft Yahei;*//*字体大小为20点*/font-size:12pt;/*字体颜色为白色*/color:dark;/*背景颜色*/background-color:white;/*边框圆角半径为5像素*/border-radius:5px;height:30;/* 边框风格 */border-style:outset;/* 边框宽度 */border-width:1px;/* 边框颜色 */border-color:rgb(216, 216, 216);}";
}
strStyle += "/*按钮停留态*/\
QPushButton:hover\
{\
/*背景颜色*/\
/*background-color:rgb(65, 127, 249);*/\
/*color:white;*/\
}\
/*按钮按下态*/\
QPushButton:pressed\
{\
/*背景颜色*/\
background-color:rgb(59, 119, 229);\
/*左内边距为3像素,让按下时字向右移动3像素*/\
padding-left:3px;\
/*上内边距为3像素,让按下时字向下移动3像素*/\
padding-top:3px;\
}";
pBtn->setStyleSheet(strStyle);
}
//************************************
// Method: setListStyleSheet
// Description:listWidget样式
// Author:xxxx
// Date:2020/10/26
// Returns: void
// Parameter: QListWidget * & listWidget
//************************************
void MDialog::setListStyleSheet(QListWidget*& listWidget)
{
QString strStyleSheet = "QListWidget{\
border:1px solid rgb(232, 232, 232); /*边界线:宽度、颜色 */\
/*background:gray;*/ /*表格背景色*/\
color:black; /*前景色:文字颜色*/\
/*margin:5px,5px,0px,50px;*/ /*上、下、左、右,间距*/}\
QListWidget::item{\
/*height:40px;*/\
padding-top:2px;\
padding-bottom:2px;\
border-bottom: 1px solid rgb(234, 234, 234);\
}";
listWidget->setStyleSheet(strStyleSheet);
}
void MDialog::setResult(int nResult)
{
m_nRaidoBtnChoose = nResult;
}
void MDialog::setDlgShowSymbol(bool bIsShow)
{
m_bIsExec = bIsShow;
}
void MDialog::onTitleCloseBtnClicked(bool)
{
m_nRaidoBtnChoose = -1;
this->close();
}
//************************************
// Method: onForceCloseBtnClicked
// Description:用户点击强制退出按钮的响应函数
// Author:xxx
// Date:2020/10/26
// Returns: void
// Parameter: bool
//************************************
void MDialog::onForceCloseBtnClicked(bool)
{
m_bIsClickedForceQuitBtn = true;
if (m_bNeedShowSecureTip)
{
m_pListWidgetLayout = new QHBoxLayout(this);
m_pListWidget = new QListWidget(this);
m_pButtonGroup = new QButtonGroup(this);
m_pButtonHLayout = new QHBoxLayout(this);
m_pListWidget->setSpacing(3);
setListStyleSheet(m_pListWidget);
QListWidgetItem* pChooseItem1 = new QListWidgetItem(m_pListWidget);
pChooseItem1->setSizeHint(QSize(20, 38));
QListWidgetItem* pChooseItem2 = new QListWidgetItem(m_pListWidget);
pChooseItem2->setSizeHint(QSize(20, 38));
m_pListWidget->addItem(pChooseItem1);
m_pListWidget->addItem(pChooseItem2);
m_pListWidget->setItemWidget(pChooseItem1, createListItemWidget(QString::fromLocal8Bit("清理本地的缓存文件,以保证文档的安全"), ""));
m_pListWidget->setItemWidget(pChooseItem2, createListItemWidget(QString::fromLocal8Bit("保留数据以供下次使用"), ""));
m_pListWidget->item(0)->setSizeHint(QSize(400 * m_dpi, 38 * m_dpi));
m_pListWidget->item(1)->setSizeHint(QSize(400 * m_dpi, 38 * m_dpi));
m_pSureQuitBtn = new QPushButton(this);
m_pSureQuitBtn->setText(QString::fromLocal8Bit("确认退出"));
m_pSureQuitBtn->setMinimumHeight(30 * m_dpi);
setButtonStyleSheet(m_pSureQuitBtn);
m_pButtonHLayout->addStretch(1);
m_pButtonHLayout->addWidget(m_pSureQuitBtn, 1);
m_pButtonHLayout->addStretch(1);
m_pListWidgetLayout->addWidget(m_pListWidget);
m_pQuitLabel->hide();
m_pForceCloseBtn->hide();
m_pCancelBtn->hide();
m_pToolTipPicture->hide();
QLabel* pSuggestionLabel = new QLabel(this);
pSuggestionLabel->setText(QString::fromLocal8Bit("即将退出账号,您需要:"));
QFont ft(QString::fromLocal8Bit("微软雅黑"), 12, 0);
pSuggestionLabel->setFont(ft);
QFont ftTtitle(QString::fromLocal8Bit("微软雅黑"), 15, 0);
m_pMainTitleLabel->setText(QString::fromLocal8Bit("安全提示"));
m_pMainTitleLabel->setAlignment(Qt::AlignHCenter);
m_pMainTitleLabel->setFont(ftTtitle);
QLayoutItem *QuitContentLayoutChild = nullptr;
while ((QuitContentLayoutChild = m_pQuitContentLayout->takeAt(0)) != 0)
{
//setParent为NULL,防止删除之后界面不消失
if (QuitContentLayoutChild->widget())
{
QuitContentLayoutChild->widget()->setParent(NULL);
}
delete QuitContentLayoutChild;
}
QLayoutItem *ForceCloseAndCancelLayoutChild = nullptr;
while ((ForceCloseAndCancelLayoutChild = m_pForceCloseAndCancelLayout->takeAt(0)) != 0)
{
//setParent为NULL,防止删除之后界面不消失
if (ForceCloseAndCancelLayoutChild->widget())
{
ForceCloseAndCancelLayoutChild->widget()->setParent(NULL);
}
delete ForceCloseAndCancelLayoutChild;
}
QVBoxLayout * pSuggestTionLayout = new QVBoxLayout(this);
pSuggestTionLayout->addWidget(pSuggestionLabel, 1);
pSuggestTionLayout->setMargin(1);
m_pMainVLayout->removeItem(m_pForceCloseAndCancelLayout);
m_pMainVLayout->removeItem(m_pQuitContentLayout);
m_pMainVLayout->addLayout(pSuggestTionLayout, 1);
m_pMainVLayout->addLayout(m_pListWidgetLayout, 6);
m_pMainVLayout->addLayout(m_pButtonHLayout, 1);
this->setMinimumSize(QSize(480 * m_dpi, 260 * m_dpi));
connect(m_pListWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(onListWidgetCurrentItemChanged(QListWidgetItem*, QListWidgetItem*)));
connect(m_pSureQuitBtn, SIGNAL(clicked(bool)), this, SLOT(onSureQuitBtnClicked(bool)));
connect(m_pButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(onButtonGroupClicked(int)));
m_pListWidget->setCurrentItem(m_pListWidget->item(0));
}
else
{
this->close();
}
}
//************************************
// Method: onListWidgetCurrentItemChanged
// Description:listWidget切换每行时响应函数
// Author:xxx
// Date:2020/10/26
// Returns: void
// Parameter: QListWidgetItem * pCurrent
// Parameter: QListWidgetItem * pPrevious
//************************************
void MDialog::onListWidgetCurrentItemChanged(QListWidgetItem* pCurrent, QListWidgetItem* pPrevious)
{
if (pCurrent)
{
m_nRaidoBtnChoose = m_pListWidget->currentRow();
QWidget* pwig = m_pListWidget->itemWidget(pCurrent); // 获取里面的QWidget
if (pwig != NULL)
{
// 方法一 获取所有
QList<QRadioButton*> pRBtnList = pwig->findChildren<QRadioButton*>();
foreach(QRadioButton *pRBtn, pRBtnList)
{
pRBtn->setChecked(true);
}
QList<QLabel*> pLabelList = pwig->findChildren<QLabel*>();
for (int nIndex = 0; nIndex < 1; nIndex++)
{
QPalette palette;
palette.setColor(QPalette::Text, QColor(65, 127, 249));
pLabelList.at(nIndex)->setPalette(palette);
}
}
}
if (pPrevious)
{
QWidget* pwig = m_pListWidget->itemWidget(pPrevious); // 获取里面的QWidget
if (pwig != NULL)
{
// 方法一 获取所有
QList<QRadioButton*> pRBtnList = pwig->findChildren<QRadioButton*>();
foreach(QRadioButton *pRBtn, pRBtnList)
{
pRBtn->setChecked(false);
}
QList<QLabel*> pLabelList = pwig->findChildren<QLabel*>();
for (int nIndex = 0; nIndex < 1; nIndex++)
{
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::black);
pLabelList.at(nIndex)->setPalette(palette);
}
}
}
}
void MDialog::onSureQuitBtnClicked(bool)
{
setResult(m_nRaidoBtnChoose);
this->close();
}
void MDialog::onButtonGroupClicked(int nClicked)
{
qDebug() << "onButtonGroupClicked:" << nClicked;
m_pListWidget->setCurrentRow(nClicked);
}
bool MDialog::eventFilter(QObject *obj, QEvent *event)
{
return QObject::eventFilter(obj, event);
}
void MDialog::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillRect(QRect(SHADOW_WIDTH, SHADOW_WIDTH, this->width() - 2 * SHADOW_WIDTH, this->height() - 2 * SHADOW_WIDTH), QBrush(Qt::white));
QColor color(50, 50, 50, 30);
for (int i = 0; i < SHADOW_WIDTH; i++)
{
color.setAlpha(120 - qSqrt(i) * 40);
painter.setPen(color);
//方角阴影边框;
painter.drawRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2);
//圆角阴影边框;
//painter.drawRoundedRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2, 3, 3);
}
}
void MDialog::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
//只在frameTopTitle区域可以拖动
if (m_pMainTitleLabel->geometry().contains(this->mapFromGlobal(QCursor::pos())))
{
//move 用的是 globalPos, 所以,需要要取出本窗口的(0,0)对应的全局坐标
//pos()返回的是父窗口中位置,不能直接用
ptStartPos_ = event->globalPos() - mapToGlobal(QPoint(0, 0));
bKeyMovePressed_ = true;
}
}
event->accept();
}
void MDialog::mouseMoveEvent(QMouseEvent *event)
{
if (bKeyMovePressed_)
this->move(event->globalPos() - this->ptStartPos_);
event->accept();
}
void MDialog::mouseReleaseEvent(QMouseEvent *event)
{
bKeyMovePressed_ = false;
event->accept();
}
SecureTipDialog::SecureTipDialog(QWidget *parent /*= Q_NULLPTR*/)
{
m_nRaidoBtnChoose = -1;
m_nButtonIndex = 0;
m_dDpi = 1;
_isleftpressed = false;
_curpos = 0;//标记鼠标左击时的位置
m_pMainTitleLabel = new QLabel(this);
m_pMainTitleLabel->setText(QString::fromLocal8Bit("退出账号"));
this->setMouseTracking(true);
Qt::WindowFlags flags = Qt::Dialog;
flags |= Qt::FramelessWindowHint;
flags |= Qt::Tool; //程序不在任务栏显示
flags |= Qt::WindowStaysOnTopHint; //置顶显示
setWindowFlags(flags);
setAttribute(Qt::WA_TranslucentBackground); //透明化
m_pMainVLayout = new QVBoxLayout(this);
m_pListWidgetLayout = new QHBoxLayout(this);
m_pButtonHLayout = new QHBoxLayout(this);
m_pQuitContentLayout = new QHBoxLayout(this);
m_pListWidget = new QListWidget(this);
m_pListWidget->setSpacing(3);
setListStyleSheet(m_pListWidget);
QFont font(QString::fromLocal8Bit("微软雅黑"), 13, 6);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75)
m_pMainTitleLabel->setFont(font);
QPalette pa;
pa.setColor(QPalette::WindowText, Qt::black);
m_pMainTitleLabel->setPalette(pa);
m_pButtonGroup = new QButtonGroup(this);
m_pTitleCloseBtn = new QPushButton(this);
m_pTitleCloseBtn->installEventFilter(this);
m_pTitleCloseBtn->setFixedWidth(20);
m_pTitleCloseBtn->setFixedHeight(20);
m_pTitleCloseBtn->setIconSize(QSize(16, 16));
m_pTitleCloseBtn->setStyleSheet("QPushButton{ border-image:url(':/p2.png');}/*QPushButton:hover{ border-image:url(':/window_close_p.png');\
background-color: qlineargradient(spread: pad, x1: 0, y1: 1, x2: 0, y2: 0, stop: 0 rgba(238, 0, 0, 128), stop: 1 rgba(238, 44, 44, 255));\
}QPushButton{border-radius:0px;color: #F0F0F0;background-color:rgba(0, 0, 0, 0);border-style:none;}*/");
m_pTitleCloseBtn->setToolTip(QString::fromLocal8Bit("关闭"));
QHBoxLayout* pTitleHLayout = new QHBoxLayout(this);
pTitleHLayout->addStretch(4);
pTitleHLayout->addWidget(m_pMainTitleLabel, 5);
pTitleHLayout->addStretch(4);
pTitleHLayout->addWidget(m_pTitleCloseBtn, 1);
//pTitleHLayout->setMargin(SHADOW_WIDTH + 8);
m_pMainVLayout->addLayout(pTitleHLayout, 1);
QLabel *blueLine = new QLabel(this);
blueLine->setMaximumHeight(1);
blueLine->setStyleSheet("background-color:rgb(227, 227, 227);");
QHBoxLayout *blueLineLayout = new QHBoxLayout(this);
blueLineLayout->addWidget(blueLine);
m_pMainVLayout->addLayout(blueLineLayout, 1);
QListWidgetItem* pChooseItem1 = new QListWidgetItem(m_pListWidget);
pChooseItem1->setSizeHint(QSize(20, 38));
QListWidgetItem* pChooseItem2 = new QListWidgetItem(m_pListWidget);
pChooseItem2->setSizeHint(QSize(20, 38));
m_pListWidget->addItem(pChooseItem1);
m_pListWidget->addItem(pChooseItem2);
m_pListWidget->setItemWidget(pChooseItem1, createListItemWidget(QString::fromLocal8Bit("清理本地的缓存文件,以保证文档的安全"), ""));
m_pListWidget->setItemWidget(pChooseItem2, createListItemWidget(QString::fromLocal8Bit("保留数据以供下次使用"), ""));
m_pListWidget->item(0)->setSizeHint(QSize(400 * m_dDpi, 38 * m_dDpi));
m_pListWidget->item(1)->setSizeHint(QSize(400 * m_dDpi, 38 * m_dDpi));
m_pListWidget->setMinimumHeight(80 * m_dDpi);
m_pListWidget->setMaximumHeight(97 * m_dDpi);
m_pSureQuitBtn = new QPushButton(this);
m_pSureQuitBtn->setText(QString::fromLocal8Bit("确认退出"));
m_pSureQuitBtn->setMinimumHeight(30 * m_dDpi);
m_pSureCancelBtn = new QPushButton(this);
m_pSureCancelBtn->setText(QString::fromLocal8Bit("取消"));
m_pSureCancelBtn->setMinimumHeight(30 * m_dDpi);
setButtonStyleSheet(m_pSureQuitBtn);
setButtonStyleSheet(m_pSureCancelBtn);
m_pButtonHLayout->addStretch(1);
m_pButtonHLayout->addWidget(m_pSureQuitBtn, 1);
m_pButtonHLayout->addWidget(m_pSureCancelBtn, 1);
m_pButtonHLayout->addStretch(1);
m_pListWidgetLayout->addWidget(m_pListWidget);
this->setStyleSheet("background-color: white");
QLabel* pSuggestionLabel = new QLabel(this);
pSuggestionLabel->setText(QString::fromLocal8Bit("即将退出账号,您需要:"));
QFont ftSu(QString::fromLocal8Bit("微软雅黑"), 11, 0);
pSuggestionLabel->setFont(ftSu);
//pSuggestionLabel->setMaximumHeight(15 * m_dDpi);
QFont ft(QString::fromLocal8Bit("微软雅黑"), 13, 0);
m_pMainTitleLabel->setFont(ft);
m_pMainTitleLabel->setText(QString::fromLocal8Bit("安全提示"));
m_pMainTitleLabel->setAlignment(Qt::AlignHCenter);
m_pQuitContentLayout->addWidget(pSuggestionLabel);
m_pQuitContentLayout->setMargin(1);
m_pMainVLayout->addLayout(m_pQuitContentLayout, 1);
m_pMainVLayout->addLayout(m_pListWidgetLayout, 5);
m_pMainVLayout->addLayout(m_pButtonHLayout, 1);
m_pMainVLayout->setMargin(SHADOW_WIDTH + 8);
this->setMinimumSize(QSize(480 * m_dDpi, 230 * m_dDpi));
this->setLayout(m_pMainVLayout);
connect(m_pTitleCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
connect(m_pListWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(onListWidgetCurrentItemChanged(QListWidgetItem*, QListWidgetItem*)));
connect(m_pSureQuitBtn, SIGNAL(clicked(bool)), this, SLOT(onSureQuitBtnClicked(bool)));
connect(m_pButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(onButtonGroupClicked(int)));
connect(m_pSureCancelBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
onListWidgetCurrentItemChanged(m_pListWidget->item(0), NULL);
}
SecureTipDialog::~SecureTipDialog()
{
qDebug() << QString::fromLocal8Bit("用户选择的是:") << getResult();
}
int SecureTipDialog::getResult()
{
return m_nRaidoBtnChoose;
}
void SecureTipDialog::setDpi(double fDpi)
{
m_dDpi = fDpi;
}
void SecureTipDialog::init()
{
m_nRaidoBtnChoose = -1;
m_pSureCancelBtn->show();
onListWidgetCurrentItemChanged(m_pListWidget->item(0), NULL);
}
QWidget* SecureTipDialog::createListItemWidget(QString strLabelTitle, QString strLabelContent)
{
QWidget* container = new QWidget(this);
QHBoxLayout* pContainerLayout = new QHBoxLayout(container);
QVBoxLayout* pLabelLayout = new QVBoxLayout(container);
QHBoxLayout* pButtonLayout = new QHBoxLayout(container);
QRadioButton* pRBChoose = new QRadioButton(container);
m_pButtonGroup->addButton(pRBChoose, m_nButtonIndex);
pRBChoose->setIconSize(QSize(30 * m_dDpi, 30 * m_dDpi));
QLabel* pLabelTitle = new QLabel(container);
pLabelTitle->setText(strLabelTitle);
QFont ft(QString::fromLocal8Bit("微软雅黑"), 11, 3);
QPalette pa;
pa.setColor(QPalette::WindowText, Qt::black);
pLabelTitle->setPalette(pa);
pLabelTitle->setFont(ft);
QPalette pa1;
pa1.setColor(QPalette::Text, QColor(170, 170, 170));
pLabelLayout->addWidget(pLabelTitle);
pButtonLayout->addWidget(pRBChoose);
pContainerLayout->addLayout(pButtonLayout, 1);
pContainerLayout->addLayout(pLabelLayout, 5);
container->setLayout(pContainerLayout);
m_nButtonIndex++;
return container;
}
void SecureTipDialog::setButtonStyleSheet(QPushButton*& pBtn)
{
QString strStyle = "";
if (pBtn->text() == QString::fromLocal8Bit("是") || pBtn->text() == QString::fromLocal8Bit("确认退出"))
{
strStyle = "QPushButton{/*font-family:Microsoft Yahei;*//*字体大小为20点*/font-size:12pt;/*字体颜色为白色*/color:white;/*背景颜色*/background-color:rgb(65, 127, 249);/*边框圆角半径为5像素*/border-radius:5px;height:30;/* 边框风格 */border-style:outset;/* 边框宽度 */border-width:1px;/* 边框颜色 */border-color:rgb(216, 216, 216);}";
}
else
{
strStyle = "QPushButton{/*font-family:Microsoft Yahei;*//*字体大小为20点*/font-size:12pt;/*字体颜色为白色*/color:dark;/*背景颜色*/background-color:white;/*边框圆角半径为5像素*/border-radius:5px;height:30;/* 边框风格 */border-style:outset;/* 边框宽度 */border-width:1px;/* 边框颜色 */border-color:rgb(216, 216, 216);}";
}
strStyle += "/*按钮停留态*/\
QPushButton:hover\
{\
/*背景颜色*/\
/*background-color:rgb(65, 127, 249);*/\
/*color:white;*/\
}\
/*按钮按下态*/\
QPushButton:pressed\
{\
/*背景颜色*/\
background-color:rgb(59, 119, 229);\
/*左内边距为3像素,让按下时字向右移动3像素*/\
padding-left:3px;\
/*上内边距为3像素,让按下时字向下移动3像素*/\
padding-top:3px;\
}";
pBtn->setStyleSheet(strStyle);
}
void SecureTipDialog::setListStyleSheet(QListWidget*& listWidget)
{
QString strStyleSheet = "QListWidget{\
border:1px solid rgb(232, 232, 232); /*边界线:宽度、颜色 */\
/*background:gray;*/ /*表格背景色*/\
color:black; /*前景色:文字颜色*/\
/*margin:5px,5px,0px,50px;*/ /*上、下、左、右,间距*/}\
QListWidget::item{\
/*height:40px;*/\
padding-top:2px;\
padding-bottom:2px;\
border-bottom: 1px solid rgb(234, 234, 234);\
}";
listWidget->setStyleSheet(strStyleSheet);
}
void SecureTipDialog::setResult(int nResult)
{
m_nRaidoBtnChoose = nResult;
}
void SecureTipDialog::onTitleCloseBtnClicked(bool)
{
m_nRaidoBtnChoose = -1;
this->close();
}
void SecureTipDialog::onListWidgetCurrentItemChanged(QListWidgetItem* pCurrent, QListWidgetItem* pPrevious)
{
if (pCurrent)
{
m_nRaidoBtnChoose = m_pListWidget->currentRow();
QWidget* pwig = m_pListWidget->itemWidget(pCurrent); // 获取里面的QWidget
if (pwig != NULL)
{
// 方法一 获取所有
QList<QRadioButton*> pRBtnList = pwig->findChildren<QRadioButton*>();
foreach(QRadioButton *pRBtn, pRBtnList)
{
pRBtn->setChecked(true);
}
QList<QLabel*> pLabelList = pwig->findChildren<QLabel*>();
for (int nIndex = 0; nIndex < 1; nIndex++)
{
QPalette palette;
palette.setColor(QPalette::Text, QColor(65, 127, 249));
pLabelList.at(nIndex)->setPalette(palette);
}
}
}
if (pPrevious)
{
QWidget* pwig = m_pListWidget->itemWidget(pPrevious); // 获取里面的QWidget
if (pwig != NULL)
{
// 方法一 获取所有
QList<QRadioButton*> pRBtnList = pwig->findChildren<QRadioButton*>();
foreach(QRadioButton *pRBtn, pRBtnList)
{
pRBtn->setChecked(false);
}
QList<QLabel*> pLabelList = pwig->findChildren<QLabel*>();
for (int nIndex = 0; nIndex < 1; nIndex++)
{
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::black);
pLabelList.at(nIndex)->setPalette(palette);
}
}
}
}
void SecureTipDialog::onSureQuitBtnClicked(bool)
{
setResult(m_nRaidoBtnChoose);
this->close();
}
void SecureTipDialog::onButtonGroupClicked(int nClicked)
{
qDebug() << "onButtonGroupClicked:" << nClicked;
m_pListWidget->setCurrentRow(nClicked);
}
bool SecureTipDialog::eventFilter(QObject *obj, QEvent *event)
{
return QObject::eventFilter(obj, event);
}
void SecureTipDialog::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
this->_isleftpressed = true;
QPoint temp = event->globalPos();
_plast = temp;
_curpos = countFlag(event->pos(), countRow(event->pos()));
event->ignore();
}
}
void SecureTipDialog::mouseMoveEvent(QMouseEvent *event)
{
int poss = countFlag(event->pos(), countRow(event->pos()));
if (_isleftpressed)//是否左击
{
QPoint ptemp = event->globalPos();
ptemp = ptemp - _plast;
if (_curpos == 22)//移动窗口
{
ptemp = ptemp + pos();
move(ptemp);
}
_plast = event->globalPos();//更新位置
}
event->ignore();
}
void SecureTipDialog::mouseReleaseEvent(QMouseEvent *event)
{
if (_isleftpressed)
_isleftpressed = false;
setCursor(Qt::ArrowCursor);
event->ignore();
}
int SecureTipDialog::countFlag(QPoint p, int row)
{
if (p.y() < MARGIN)
return 10 + row;
else if (p.y() > this->height() - MARGIN)
return 30 + row;
else
return 20 + row;
}
int SecureTipDialog::countRow(QPoint p)
{
return (p.x() < MARGIN) ? 1 : (p.x() > (this->width() - MARGIN) ? 3 : 2);
}
void SecureTipDialog::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillRect(QRect(SHADOW_WIDTH, SHADOW_WIDTH, this->width() - 2 * SHADOW_WIDTH, this->height() - 2 * SHADOW_WIDTH), QBrush(Qt::white));
QColor color(50, 50, 50, 30);
for (int i = 0; i < SHADOW_WIDTH; i++)
{
color.setAlpha(120 - qSqrt(i) * 40);
painter.setPen(color);
//方角阴影边框;
painter.drawRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2);
//圆角阴影边框;
//painter.drawRoundedRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2, 3, 3);
}
}
MessageDialog::MessageDialog(QWidget *parent /*= Q_NULLPTR*/)
: QDialog(parent)
{
m_nResult = -1;
_isleftpressed = false;
_curpos = 0;//标记鼠标左击时的位置
m_dDpi = 1;
m_pMainTitleLabel = new QLabel(this);
m_pMainTitleLabel->setText(QString::fromLocal8Bit("升级提示"));
this->setMouseTracking(true);
Qt::WindowFlags flags = Qt::Dialog;
flags |= Qt::FramelessWindowHint;
flags |= Qt::Tool; //程序不在任务栏显示
flags |= Qt::WindowStaysOnTopHint; //置顶显示
flags |= Qt::Popup;
setWindowFlags(flags);
setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground); //透明化
m_pMainVLayout = new QVBoxLayout(this);
QVBoxLayout* pQuitContentLayout = new QVBoxLayout(this);
QHBoxLayout* pTitleHLayout = new QHBoxLayout(this);
QFont font(QString::fromLocal8Bit("微软雅黑"), 13, 6);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75)
QPalette pa;
pa.setColor(QPalette::WindowText, Qt::black);
m_pMainTitleLabel->setFont(font);
m_pMainTitleLabel->setPalette(pa);
m_pTitleCloseBtn = new QPushButton(this);
m_pTitleCloseBtn->installEventFilter(this);
m_pTitleCloseBtn->setMinimumWidth(18);
m_pTitleCloseBtn->setMinimumHeight(18);
m_pTitleCloseBtn->setIconSize(QSize(16, 16));
/*m_pTitleCloseBtn->setStyleSheet("QPushButton{ border-image:url('../dialog1/window_close_n.png');}QPushButton:hover{ border-image:url('../dialog1/window_close_p.png');\
background-color: qlineargradient(spread: pad, x1: 0, y1: 1, x2: 0, y2: 0, stop: 0 rgba(238, 0, 0, 128), stop: 1 rgba(238, 44, 44, 255));\
}QPushButton{border-radius:0px;color: #F0F0F0;background-color:rgba(0, 0, 0, 0);border-style:none;}");*/
m_pTitleCloseBtn->setStyleSheet("QPushButton{border-image:url('../dialog1/p2.png');}");
pTitleHLayout->addWidget(m_pMainTitleLabel, 14);
pTitleHLayout->addStretch(10);
pTitleHLayout->addWidget(m_pTitleCloseBtn, 1);
m_pMainVLayout->addLayout(pTitleHLayout, 1);
QVBoxLayout* blueLayout = new QVBoxLayout(this);
QLabel *blueLine = new QLabel(this);
blueLine->setMaximumHeight(1);
blueLine->setStyleSheet("background-color:rgb(227, 227, 227);");
blueLayout->addWidget(blueLine);
m_pMainVLayout->addLayout(blueLayout, 1);
m_pQuitLabel = new QLabel(this);
m_pQuitLabel->setText(QString::fromLocal8Bit("检测到软件有新版,是否更新到最新版以获得更优的使用体验!"));
QFont fontQuitLabel(QString::fromLocal8Bit("微软雅黑"), 11, 10);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75)
m_pQuitLabel->setFont(fontQuitLabel);
QPalette paQuitLabel;
paQuitLabel.setColor(QPalette::Text, QColor(66, 66, 66, 255));
m_pQuitLabel->setPalette(paQuitLabel);
m_pForceCloseBtn = new QPushButton(this);
m_pForceCloseBtn->setText(QString::fromLocal8Bit("立即升级"));
m_pForceCloseBtn->setMinimumSize(QSize(80 * m_dDpi, 30 * m_dDpi));
m_pForceCloseBtn->setChecked(true);
m_pCancelBtn = new QPushButton(this);
m_pCancelBtn->setText(QString::fromLocal8Bit("下次再说"));
m_pCancelBtn->setMinimumSize(QSize(80 * m_dDpi, 30 * m_dDpi));
m_pForceCloseAndCancelLayout = new QHBoxLayout(this);
setButtonStyleSheet(m_pForceCloseBtn, true);
setButtonStyleSheet(m_pCancelBtn, false);
m_pForceCloseAndCancelLayout->addStretch(13);
m_pForceCloseAndCancelLayout->addWidget(m_pForceCloseBtn);
m_pForceCloseAndCancelLayout->addWidget(m_pCancelBtn);
m_pForceCloseAndCancelLayout->addStretch(1);
pQuitContentLayout->addWidget(m_pQuitLabel);
m_pMainVLayout->addLayout(pQuitContentLayout, 3);
m_pMainVLayout->addLayout(m_pForceCloseAndCancelLayout, 1);
m_pMainVLayout->addStretch(1);
this->setMinimumSize(QSize(430 * m_dDpi, 160 * m_dDpi));
this->setStyleSheet("background-color: white");
m_pMainVLayout->setMargin(SHADOW_WIDTH + 8);
this->setLayout(m_pMainVLayout);
connect(m_pTitleCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
connect(m_pForceCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onForceCloseBtnClicked(bool)));
connect(m_pCancelBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
}
MessageDialog::~MessageDialog()
{
qDebug() << "user select:" << getResult();
}
int MessageDialog::getResult()
{
return m_nResult;
}
void MessageDialog::setTitleText(QString strTitle)
{
m_pMainTitleLabel->setText(strTitle);
}
void MessageDialog::setContentText(QString strContent)
{
m_pQuitLabel->setText(strContent);
}
void MessageDialog::setBtnOneText(QString strBtn1text)
{
m_pForceCloseBtn->setText(strBtn1text);
}
void MessageDialog::setBtnTwoText(QString strBtn2text)
{
m_pCancelBtn->setText(strBtn2text);
}
void MessageDialog::setButtonStyleSheet(QPushButton*& pBtn, bool bBlue)
{
QString strStyle = "";
if (bBlue)
{
strStyle = "QPushButton{/*font-family:Microsoft Yahei;*//*字体大小为20点*/font-size:11pt;/*字体颜色为白色*/color:white;/*背景颜色*/background-color:rgb(65, 127, 249);/*边框圆角半径为5像素*/border-radius:5px;/* 边框风格 */border-style:outset;/* 边框宽度 */border-width:1px;/* 边框颜色 */border-color:rgb(216, 216, 216);}";
}
else
{
strStyle = "QPushButton{/*font-family:Microsoft Yahei;*//*字体大小为20点*/font-size:11pt;/*字体颜色为白色*/color:dark;/*背景颜色*/background-color:white;/*边框圆角半径为5像素*/border-radius:5px;/* 边框风格 */border-style:outset;/* 边框宽度 */border-width:1px;/* 边框颜色 */border-color:rgb(216, 216, 216);}";
}
strStyle += "/*按钮停留态*/\
QPushButton:hover{\
/*背景颜色*/\
/*background-color:rgb(65, 127, 249);*/\
/*color:white;*/\
}\
/*按钮按下态*/\
QPushButton:pressed {\
/*背景颜色*/\
background-color:rgb(59, 119, 229);\
/*左内边距为3像素,让按下时字向右移动3像素*/\
padding-left:3px;\
/*上内边距为3像素,让按下时字向下移动3像素*/\
padding-top:3px;\
}";
pBtn->setStyleSheet(strStyle);
}
void MessageDialog::setResult(int nResult)
{
m_nResult = nResult;
}
void MessageDialog::onTitleCloseBtnClicked(bool)
{
setResult(0);
this->close();
}
void MessageDialog::onForceCloseBtnClicked(bool)
{
setResult(1);
this->close();
}
bool MessageDialog::eventFilter(QObject *obj, QEvent *event)
{
return QObject::eventFilter(obj, event);
}
void MessageDialog::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
this->_isleftpressed = true;
QPoint temp = event->globalPos();
_plast = temp;
_curpos = countFlag(event->pos(), countRow(event->pos()));
event->ignore();
}
}
void MessageDialog::mouseMoveEvent(QMouseEvent *event)
{
int poss = countFlag(event->pos(), countRow(event->pos()));
if (_isleftpressed)//是否左击
{
QPoint ptemp = event->globalPos();
ptemp = ptemp - _plast;
if (_curpos == 22)//移动窗口
{
ptemp = ptemp + pos();
move(ptemp);
}
_plast = event->globalPos();//更新位置
}
event->ignore();
}
void MessageDialog::mouseReleaseEvent(QMouseEvent *event)
{
if (_isleftpressed)
_isleftpressed = false;
setCursor(Qt::ArrowCursor);
event->ignore();
}
int MessageDialog::countFlag(QPoint p, int row)
{
if (p.y() < MARGIN)
return 10 + row;
else if (p.y() > this->height() - MARGIN)
return 30 + row;
else
return 20 + row;
}
int MessageDialog::countRow(QPoint p)
{
return (p.x() < MARGIN) ? 1 : (p.x() > (this->width() - MARGIN) ? 3 : 2);
}
void MessageDialog::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillRect(QRect(SHADOW_WIDTH, SHADOW_WIDTH, this->width() - 2 * SHADOW_WIDTH, this->height() - 2 * SHADOW_WIDTH), QBrush(Qt::white));
QColor color(50, 50, 50, 30);
for (int i = 0; i < SHADOW_WIDTH; i++)
{
color.setAlpha(120 - qSqrt(i) * 40);
painter.setPen(color);
//方角阴影边框;
painter.drawRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2);
//圆角阴影边框;
//painter.drawRoundedRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2, 4, 4);
}
}
调用:
main.cpp
#include "mdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// MDialog w;
// w.show();
MessageDialog dlg;
dlg.exec();
return a.exec();
}