#include #include #include #include #include #_qt自定义dialog">搜索查看编辑修改首页UNITYNODEJSPYTHONAIGITPHPGOCEF3JAVAHTMLCSS搜索2023面试高手 这个屌丝很懒,什么也没留下! 关注作者热门标签jqueryHTMLCSSPHPASPPYTHONGOAICC++C#PHOTOSHOPUNITYiOSandroidvuexml爬虫SEOLINUXWINDOWSJAVAMFCCEF3CADNODEJSGITPyppeteerarticle热门文章1one piece_娜美_01_one piece hentai2CrossOver软件2023破解激活码_crossover-wine注册3Spring Boot(四):Thymeleaf 使用详解_spring thymeleaf4NX+Ubuntu18.04+ROS Realsense(RealSenseD435i )的安装与使用_realsense安装5记录:rosdep update6js通过a标签的方式下载文件并对其重命名的完整方案_a标签下载文件重命名7JavaScript中的Array.prototype.forEach()方法(简介+重写)_js array.prototype.foreach8leetcode 94 二叉树的中序遍历(java)_lecode中树的输入root = [1,null,3,2,4,null,5,6]是怎么转化成节点的9R语言入门笔记2.110c#--正则表达式(项目常用)_c# 正则表达式 数字当前位置: article > 正文 QT自定义弹窗dialog(纯代码编写,无Ui文件)_qt自定义dialog 作者:2023面试高手 | 2024-02-16 15:56:40 赞踩qt自定义dialog 自定义弹窗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();} 声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/94938推荐阅读articlebootstrap中的模态框(modal,弹出层)_role="dialog...bootstrap中的模态框(modal),不同于Tooltips,模态框以弹出对话框的形式出现,具有最小和最实用的功能... 赞踩articleBootstrap Blazor 实战 Dialog 对话框组件快速入门_bootstrap dia...Dialog 对话框组件快速入门_bootstrap dialog 组件bootstrap dialog 组件 ... 赞踩article封装bootstrap-dialog.js中的3个提示框...这里主要对项目中常用的3个提示框进行了封装,成功提示框,错误提示框和选择提示框://弹出错误提示的登录框$.showEr... 赞踩articlebootstrap3-dialog打开嵌套iframe窗口_bootstrap dialog.set... bootstrap3-dialog是一款第三方提示框插件,但是使用过程中感觉对打开新的远程页面不太友好,而打开表单嵌套... 赞踩articlebootstrap3-dialog 使用说明...bootstrap3-dialog 使用模态对话框 bootstrap3-dialog ... 赞踩article解决Element Plus中Select在El Dialog里层级过低的问题(修改select选项...在使用Element Plus的``组件时,很多情况下我们需要在对话框内部使用``组件,以提供用户选择的功能。然而,由于... 赞踩article(css)自定义el-dialog对话框添加背景图片_el-dialog 自定义背景图...【代码】(css)修改el-dialog对话框样式。_el-dialog 自定义背景图el-dialog 自定义背景图 ... 赞踩articleelement的dialog弹框的背景色_el-dialog 背景色...对于弹框的背景色,我们会发现,平时的background-color和background修改的颜色不是弹框部分,而是后... 赞踩articleVUE页面设置背景图片,显示el-dialog时背景会变灰色_vue dialog 标题背景颜色...<el-dialog title="提示" :visible.sync="dialogVisible" ... 赞踩articleel-dialog去除暗色背景或修改其底色_vue 清除背景 el- dialog backgrou...直接在el-dialog标签上 设置modal true 表示要底层 false 表示不要。我们就能直接用通过父节点的穿... 赞踩articleElementUI中Dialog 对话框背景色等样式修改_el-dialog 美化...ElementUI中Dialog 对话框修改背景色等样式修改1. 不能将使用scoped,既不能将对话框样式放到带有sc... 赞踩articleel-dialog背景色、文字颜色修改以及最终实现效果_el-dialog title颜色...el-dialog背景色、文字颜色修改以及最终实现效果_el-dialog title颜色el-dialog title... 赞踩articleelement dialog 设置背景色_element的弹框背景颜色.../*弹出层设置背景色头部*/.el-dialog__header{background:#ecf5ff;}/*弹出层设置... 赞踩articleelement修改el-dialog的背景色_el-dialog 背景色....el-dialog {background-color: rgb(160, 207, 255);}_el-dialog... 赞踩articleelementUI给dialog标题添加icon图标_el-dialog icon图标...利用插巢slot给dialog标题添加icon图标<el-dialog title="错误信息提示" :visible=... 赞踩article【前端】element自定义el-dialog标题样式,实现title部分放置按钮_el-dialo...element自定义el-dialog标题样式,实现title部分放置一个或多个按钮_el-dialog 自定义titl... 赞踩articleelementUI给el-dialog标题添加自定义图片_el-dialog 头部自定义...利用插巢slot给el-dialog弹框标题添加icon图标或自定义图片<el-dialog :title="title... 赞踩article对话框中加入标签页的5种方法_el-dialog 带标签窗口...(2008-07-08 15:25 博客搬家拾遗)标签页是MFC控件里有一点难用的东西,今天看到了一篇相关文章,觉得写得... 赞踩articleelementui在Dialog对话框标题前面加图标的实现_el-dialog header添加图标...elementui在Dialog对话框标题前面加图标的实现_el-dialog header添加图标el-dialog ... 赞踩article如何给element-ui的表格头部加按钮、加图标、加自定义内容_el-dialog 头部加按钮...关于如何给element-ui的表格头部加按钮、加图标、加自定义内容等等,话不多说上图:代码:嗯,就这样了。_el-di... 赞踩相关标签modalhtml5bootstrap.netbootstrap3-dialog模态窗口iframe对话框前端vue.jselementuicsshtmljsvuejavascriptcss3
赞
踩
自定义弹窗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();}