#include #include #include #include #include #_qt自定义dialog">
当前位置:   article > 正文

QT自定义弹窗dialog(纯代码编写,无Ui文件)_qt自定义dialog

qt自定义dialog

自定义弹窗dialog

MDialog.h

  1. #pragma once
  2. #include <QDialog>
  3. //#include "ui_MDialog.h"
  4. #include <QGroupBox>
  5. #include <QListWidget>
  6. #include <QHBoxLayout>
  7. #include <QWidget>
  8. #include <QRadioButton>
  9. #include <QLabel>
  10. #include <QListView>
  11. #include <QTableWidget>
  12. #include <QPushButton>
  13. #include <QFile>
  14. #include <QMouseEvent>
  15. #include <QPainter>
  16. #include <qmath.h>
  17. #include <QDebug>
  18. #include <QButtonGroup>
  19. #include <QApplication>
  20. #include<windows.h>
  21. class MDialog : public QDialog
  22. {
  23. Q_OBJECT
  24. public:
  25. MDialog(QWidget *parent = Q_NULLPTR);
  26. ~MDialog();
  27. public:
  28. int getResult(); //提供给外部用户点击的结果
  29. bool getForceQuitResult();
  30. void setSecureTipWidgetShow(bool bChoose);
  31. void ShowSureQuit();
  32. void setQuitLabel(QString str);//设置窗口内容
  33. void setDialogTitle(QString str); //设置弹窗的标题
  34. void init();
  35. void mQuit();
  36. bool mIsShow();
  37. void setDlgShowSymbol(bool bIsShow);
  38. private:
  39. QWidget* createListItemWidget(QString strLabelTitlr, QString strLabelContent); //创建listwidgetItem成员
  40. void setButtonStyleSheet(QPushButton*& pBtn);//设置按钮样式
  41. void setListStyleSheet(QListWidget*& listWidget);//设置ListWidget样式
  42. void setResult(int nResult);//记录用户选择的结果
  43. void setCursorType(int flag);
  44. public slots:
  45. void onTitleCloseBtnClicked(bool);
  46. void onForceCloseBtnClicked(bool);
  47. void onListWidgetCurrentItemChanged(QListWidgetItem* pCurrent, QListWidgetItem* pPrevious);
  48. void onSureQuitBtnClicked(bool);
  49. void onButtonGroupClicked(int nClicked);
  50. protected:
  51. bool eventFilter(QObject *obj, QEvent *event);
  52. void mousePressEvent(QMouseEvent *event);
  53. void mouseMoveEvent(QMouseEvent *event);
  54. void mouseReleaseEvent(QMouseEvent *event);
  55. int countFlag(QPoint p, int row);
  56. int countRow(QPoint p);
  57. //界面重绘响应
  58. virtual void paintEvent(QPaintEvent *event);
  59. private:
  60. //Ui::MDialog ui;
  61. QVBoxLayout* m_pMainVLayout; //dialog主布局
  62. QListWidget* m_pListWidget; //选项列表
  63. QHBoxLayout* m_pListWidgetLayout;
  64. QHBoxLayout* m_pButtonHLayout;
  65. QHBoxLayout* m_pForceCloseAndCancelLayout;
  66. QHBoxLayout* m_pQuitContentLayout; //提示内容布局
  67. QLabel *m_pQuitLabel; //强制关闭提示信息
  68. QPushButton* m_pForceCloseBtn; //强制关闭按钮
  69. QPushButton* m_pCancelBtn; //取消按钮
  70. QPushButton* m_pTitleCloseBtn; //关闭窗口按钮
  71. QLabel* m_pMainTitleLabel; //标题
  72. QPushButton* m_pSureQuitBtn;//确认退出
  73. QLabel* m_pToolTipPicture; //提示图标
  74. QButtonGroup* m_pButtonGroup;
  75. int m_nButtonIndex;
  76. int m_nRaidoBtnChoose; //即将退出账号的退出选择
  77. bool m_bNeedShowSecureTip; //是否需要弹出安全提示退出界面
  78. bool m_bIsClickedForceQuitBtn; //是否按下强制退出按钮
  79. double m_dpi;
  80. bool m_bIsExec; //是否已经弹窗标识
  81. //拖动变量
  82. bool bKeyMovePressed_; //拖拽窗口标记
  83. QPoint ptStartPos_;
  84. };
  85. class SecureTipDialog : public QDialog
  86. {
  87. Q_OBJECT
  88. public:
  89. SecureTipDialog(QWidget *parent = Q_NULLPTR);
  90. ~SecureTipDialog();
  91. public:
  92. QListWidget* m_pListWidget; //选项列表
  93. QPushButton* m_pSureCancelBtn;//取消
  94. public:
  95. int getResult(); //提供给外部用户点击的结果
  96. void setDpi(double fDpi);
  97. void init();
  98. private:
  99. QWidget* createListItemWidget(QString strLabelTitlr, QString strLabelContent); //创建listwidgetItem成员
  100. void setButtonStyleSheet(QPushButton*& pBtn);//设置按钮样式
  101. void setListStyleSheet(QListWidget*& listWidget);//设置ListWidget样式
  102. void setResult(int nResult);//记录用户选择的结果
  103. public slots:
  104. void onTitleCloseBtnClicked(bool);
  105. void onListWidgetCurrentItemChanged(QListWidgetItem* pCurrent, QListWidgetItem* pPrevious);
  106. void onSureQuitBtnClicked(bool);
  107. void onButtonGroupClicked(int nClicked);
  108. protected:
  109. bool eventFilter(QObject *obj, QEvent *event);
  110. void mousePressEvent(QMouseEvent *event);
  111. void mouseMoveEvent(QMouseEvent *event);
  112. void mouseReleaseEvent(QMouseEvent *event);
  113. int countFlag(QPoint p, int row);
  114. int countRow(QPoint p);
  115. //界面重绘响应
  116. virtual void paintEvent(QPaintEvent *event);
  117. private:
  118. //Ui::MDialog ui;
  119. QVBoxLayout* m_pMainVLayout; //dialog主布局
  120. QHBoxLayout* m_pListWidgetLayout;
  121. QHBoxLayout* m_pButtonHLayout;
  122. QPushButton* m_pTitleCloseBtn; //关闭窗口按钮
  123. QLabel* m_pMainTitleLabel; //标题
  124. QPushButton* m_pSureQuitBtn;//确认退出
  125. QHBoxLayout* m_pQuitContentLayout;//提示label布局
  126. QButtonGroup* m_pButtonGroup;
  127. int m_nButtonIndex;
  128. //标题拖动、双击最大化
  129. QPoint mDragPos;
  130. bool _isleftpressed;
  131. int _curpos;
  132. QPoint _plast;
  133. int m_nRaidoBtnChoose; //即将退出账号的退出选择
  134. double m_dDpi;
  135. };
  136. class MessageDialog : public QDialog
  137. {
  138. Q_OBJECT
  139. public:
  140. MessageDialog(QWidget *parent = Q_NULLPTR);
  141. ~MessageDialog();
  142. public:
  143. int getResult();
  144. void setTitleText(QString strTitle);
  145. void setContentText(QString strContent);
  146. void setBtnOneText(QString strBtn1text);
  147. void setBtnTwoText(QString strBtn2text);
  148. private:
  149. void setButtonStyleSheet(QPushButton*& pBtn, bool bBlue);
  150. void setResult(int nResult);
  151. public slots:
  152. void onTitleCloseBtnClicked(bool);
  153. void onForceCloseBtnClicked(bool);
  154. protected:
  155. bool eventFilter(QObject *obj, QEvent *event);
  156. void mousePressEvent(QMouseEvent *event);
  157. void mouseMoveEvent(QMouseEvent *event);
  158. void mouseReleaseEvent(QMouseEvent *event);
  159. int countFlag(QPoint p, int row);
  160. int countRow(QPoint p);
  161. //界面重绘响应
  162. virtual void paintEvent(QPaintEvent *event);
  163. private:
  164. QVBoxLayout* m_pMainVLayout; //dialog主布局
  165. QHBoxLayout* m_pForceCloseAndCancelLayout;
  166. QLabel *m_pQuitLabel; //强制关闭提示信息
  167. QPushButton* m_pForceCloseBtn; //强制关闭按钮
  168. QPushButton* m_pCancelBtn; //取消按钮
  169. QPushButton* m_pTitleCloseBtn; //关闭窗口按钮
  170. QLabel* m_pMainTitleLabel; //标题
  171. //标题拖动、双击最大化
  172. QPoint mDragPos;
  173. bool _isleftpressed;
  174. int _curpos;
  175. QPoint _plast;
  176. int m_nResult;
  177. double m_dDpi;
  178. };

MDialog.cpp

  1. #include "MDialog.h"
  2. #define MARGIN 5 //四个角的长度
  3. #define SHADOW_WIDTH 10 //边框阴影宽度
  4. MDialog::MDialog(QWidget *parent)
  5. : QDialog(parent)
  6. {
  7. m_bIsExec = true;
  8. bKeyMovePressed_ = false;
  9. m_nRaidoBtnChoose = -1;
  10. m_nButtonIndex = 0;
  11. m_bNeedShowSecureTip = true;
  12. m_bIsClickedForceQuitBtn = false;
  13. m_dpi = 1;
  14. m_pMainVLayout = new QVBoxLayout(this);
  15. QHBoxLayout* pTitleHLayout = new QHBoxLayout(this);
  16. m_pQuitContentLayout = new QHBoxLayout(this);
  17. m_pMainTitleLabel = new QLabel(this);
  18. m_pMainTitleLabel->setText(QString::fromLocal8Bit("退出账号"));
  19. this->setMouseTracking(true);
  20. Qt::WindowFlags flags = Qt::Dialog;
  21. flags |= Qt::FramelessWindowHint;
  22. flags |= Qt::Tool; //程序不在任务栏显示
  23. flags |= Qt::WindowStaysOnTopHint; //置顶显示
  24. setWindowFlags(flags);
  25. setAttribute(Qt::WA_TranslucentBackground);
  26. //this->setStyleSheet("QDialog{border-radius:15px;}");
  27. QFont font(QString::fromLocal8Bit("微软雅黑"), 13, 6);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75
  28. m_pMainTitleLabel->setFont(font);
  29. QPalette pa;
  30. pa.setColor(QPalette::WindowText, Qt::black);
  31. m_pMainTitleLabel->setPalette(pa);
  32. m_pTitleCloseBtn = new QPushButton(this);
  33. m_pTitleCloseBtn->installEventFilter(this);
  34. m_pTitleCloseBtn->setMaximumWidth(12 * m_dpi);
  35. m_pTitleCloseBtn->setMaximumHeight(12 * m_dpi);
  36. m_pTitleCloseBtn->setIconSize(QSize(12 * m_dpi, 12 * m_dpi));
  37. m_pTitleCloseBtn->setToolTip(QString::fromLocal8Bit("关闭"));
  38. /*m_pTitleCloseBtn->setStyleSheet("QPushButton{ border-image:url('../dialog1/p2.png');}QPushButton:hover{ border-image:url('../dialog1/p2.png');\
  39. 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));\
  40. }QPushButton{border-radius:0px;color: #F0F0F0;background-color:rgba(0, 0, 0, 0);border-style:none;}");*/
  41. m_pTitleCloseBtn->setStyleSheet("QPushButton{ border-image:url('../dialog1/p2.png');}");
  42. QVBoxLayout* pTilteLayout = new QVBoxLayout(this);
  43. pTitleHLayout->addWidget(m_pMainTitleLabel);
  44. pTitleHLayout->addWidget(m_pTitleCloseBtn);
  45. pTitleHLayout->setMargin(1);
  46. pTilteLayout->addLayout(pTitleHLayout, 1);
  47. QLabel *blueLine = new QLabel(this);
  48. blueLine->setMaximumHeight(1);
  49. blueLine->setStyleSheet("background-color:rgb(227, 227, 227);");
  50. pTilteLayout->addWidget(blueLine);
  51. pTilteLayout->setMargin(1);
  52. m_pMainVLayout->addLayout(pTilteLayout);
  53. m_pQuitLabel = new QLabel(this);
  54. m_pQuitLabel->setWordWrap(true);
  55. QString text;
  56. text.append("<p style='line-height:20px'>").append(QString::fromLocal8Bit("此操作将强制关闭当前打开的文档,可能会"))
  57. .append("\n\n").append("<p style='line-height:20px'>").append(QString::fromLocal8Bit("造成数据丢失, 是否继续 ?")).append("</p>");
  58. m_pQuitLabel->setText(text);
  59. QFont fontQuitLabel(QString::fromLocal8Bit("微软雅黑"), 11, 3);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75
  60. m_pQuitLabel->setFont(fontQuitLabel);
  61. QPalette paQuitLabel;
  62. paQuitLabel.setColor(QPalette::Text, QColor(66, 66, 66, 255));
  63. m_pQuitLabel->setPalette(paQuitLabel);
  64. //添加图片
  65. m_pToolTipPicture = new QLabel(this);
  66. m_pToolTipPicture->setMinimumSize(QSize(30 * m_dpi, 30 * m_dpi));
  67. QPixmap pixmapTask("../dialog1/p1.png");
  68. m_pToolTipPicture->setPixmap(pixmapTask);
  69. m_pToolTipPicture->setAlignment(Qt::AlignCenter);
  70. m_pQuitContentLayout->addWidget(m_pToolTipPicture, 1);
  71. m_pQuitContentLayout->addWidget(m_pQuitLabel, 3);
  72. m_pMainVLayout->addLayout(m_pQuitContentLayout, 5);
  73. m_pForceCloseBtn = new QPushButton(this);
  74. m_pForceCloseBtn->setText(QString::fromLocal8Bit("是"));
  75. m_pForceCloseBtn->setMinimumSize(QSize(100 * m_dpi, 30 * m_dpi));
  76. //m_pForceCloseBtn->setChecked(true);
  77. m_pCancelBtn = new QPushButton(this);
  78. m_pCancelBtn->setText(QString::fromLocal8Bit("否"));
  79. m_pCancelBtn->setChecked(true);
  80. m_pCancelBtn->setMinimumSize(QSize(100 * m_dpi, 30 * m_dpi));
  81. m_pForceCloseAndCancelLayout = new QHBoxLayout(this);
  82. setButtonStyleSheet(m_pForceCloseBtn);
  83. setButtonStyleSheet(m_pCancelBtn);
  84. m_pForceCloseAndCancelLayout->addStretch(6);
  85. m_pForceCloseAndCancelLayout->addWidget(m_pForceCloseBtn);
  86. m_pForceCloseAndCancelLayout->addWidget(m_pCancelBtn);
  87. m_pForceCloseAndCancelLayout->addStretch(1);
  88. m_pMainVLayout->addLayout(m_pForceCloseAndCancelLayout, 1);
  89. m_pMainVLayout->addStretch(1);
  90. this->setMinimumSize(QSize(430 * m_dpi, 180 * m_dpi));
  91. this->setStyleSheet("background-color: white");
  92. m_pMainVLayout->setMargin(SHADOW_WIDTH + 8);
  93. this->setLayout(m_pMainVLayout);
  94. connect(m_pTitleCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
  95. connect(m_pForceCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onForceCloseBtnClicked(bool)));
  96. connect(m_pCancelBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
  97. }
  98. MDialog::~MDialog()
  99. {
  100. qDebug() <<QString::fromLocal8Bit("析构函数,用户选择了:") << getResult();
  101. }
  102. int MDialog::getResult()
  103. {
  104. setDlgShowSymbol(false);
  105. return m_nRaidoBtnChoose;
  106. }
  107. bool MDialog::getForceQuitResult()
  108. {
  109. setDlgShowSymbol(false);
  110. return m_bIsClickedForceQuitBtn;
  111. }
  112. void MDialog::setSecureTipWidgetShow(bool bChoose)
  113. {
  114. m_bNeedShowSecureTip = bChoose;
  115. }
  116. //************************************
  117. // Method: ShowSureQuit
  118. // Description: 只显示安全提示页面
  119. // Author: xxxx
  120. // Date: 2020/10/26
  121. // Returns: void
  122. //************************************
  123. void MDialog::ShowSureQuit()
  124. {
  125. m_pForceCloseBtn->clicked(true);
  126. }
  127. void MDialog::setQuitLabel(QString str)
  128. {
  129. m_pQuitLabel->setText(str);
  130. }
  131. void MDialog::setDialogTitle(QString str)
  132. {
  133. m_pMainTitleLabel->setText(str);
  134. }
  135. void MDialog::init()
  136. {
  137. m_nRaidoBtnChoose = -1;
  138. setDlgShowSymbol(true);
  139. }
  140. void MDialog::mQuit()
  141. {
  142. m_nRaidoBtnChoose = -1;
  143. this->close();
  144. }
  145. bool MDialog::mIsShow()
  146. {
  147. return m_bIsExec;
  148. }
  149. //************************************
  150. // Method: createListItemWidget
  151. // Description: 整个函数是为了组合成listWidget的一行
  152. // Author: xxx
  153. // Date:2020/10/26
  154. // Returns: ListWidget的每行内容
  155. // Parameter: QString strLabelTitle:窗口标题
  156. // Parameter: QString strLabelContent:窗口内容
  157. //************************************
  158. QWidget* MDialog::createListItemWidget(QString strLabelTitle, QString strLabelContent)
  159. {
  160. QWidget* container = new QWidget(this);
  161. QHBoxLayout* pContainerLayout = new QHBoxLayout(container);
  162. QVBoxLayout* pLabelLayout = new QVBoxLayout(container);
  163. QHBoxLayout* pButtonLayout = new QHBoxLayout(container);
  164. QRadioButton* pRBChoose = new QRadioButton(container);
  165. m_pButtonGroup->addButton(pRBChoose, m_nButtonIndex);
  166. pRBChoose->setIconSize(QSize(30, 30));
  167. QLabel* pLabelTitle = new QLabel(container);
  168. pLabelTitle->setText(strLabelTitle);
  169. QFont ft(QString::fromLocal8Bit("微软雅黑"), 11, 3);
  170. QPalette pa;
  171. pa.setColor(QPalette::WindowText, QColor(66, 66, 66, 255));
  172. pLabelTitle->setPalette(pa);
  173. pLabelTitle->setFont(ft);
  174. pLabelLayout->addWidget(pLabelTitle);
  175. pButtonLayout->addWidget(pRBChoose);
  176. pContainerLayout->addLayout(pButtonLayout, 1);
  177. pContainerLayout->addLayout(pLabelLayout, 5);
  178. container->setLayout(pContainerLayout);
  179. m_nButtonIndex++;
  180. return container;
  181. }
  182. //************************************
  183. // Method: setButtonStyleSheet
  184. // Description:按钮的样式
  185. // Author:xxx
  186. // Date:2020/10/26
  187. // Returns: void
  188. // Parameter: QPushButton * & pBtn
  189. //************************************
  190. void MDialog::setButtonStyleSheet(QPushButton*& pBtn)
  191. {
  192. QString strStyle = "";
  193. if (pBtn->text() == QString::fromLocal8Bit("否") || pBtn->text() == QString::fromLocal8Bit("确认退出"))
  194. {
  195. 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);}";
  196. }
  197. else
  198. {
  199. 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);}";
  200. }
  201. strStyle += "/*按钮停留态*/\
  202. QPushButton:hover\
  203. {\
  204. /*背景颜色*/\
  205. /*background-color:rgb(65, 127, 249);*/\
  206. /*color:white;*/\
  207. }\
  208. /*按钮按下态*/\
  209. QPushButton:pressed\
  210. {\
  211. /*背景颜色*/\
  212. background-color:rgb(59, 119, 229);\
  213. /*左内边距为3像素,让按下时字向右移动3像素*/\
  214. padding-left:3px;\
  215. /*上内边距为3像素,让按下时字向下移动3像素*/\
  216. padding-top:3px;\
  217. }";
  218. pBtn->setStyleSheet(strStyle);
  219. }
  220. //************************************
  221. // Method: setListStyleSheet
  222. // Description:listWidget样式
  223. // Author:xxxx
  224. // Date:2020/10/26
  225. // Returns: void
  226. // Parameter: QListWidget * & listWidget
  227. //************************************
  228. void MDialog::setListStyleSheet(QListWidget*& listWidget)
  229. {
  230. QString strStyleSheet = "QListWidget{\
  231. border:1px solid rgb(232, 232, 232); /*边界线:宽度、颜色 */\
  232. /*background:gray;*/ /*表格背景色*/\
  233. color:black; /*前景色:文字颜色*/\
  234. /*margin:5px,5px,0px,50px;*/ /*上、下、左、右,间距*/}\
  235. QListWidget::item{\
  236. /*height:40px;*/\
  237. padding-top:2px;\
  238. padding-bottom:2px;\
  239. border-bottom: 1px solid rgb(234, 234, 234);\
  240. }";
  241. listWidget->setStyleSheet(strStyleSheet);
  242. }
  243. void MDialog::setResult(int nResult)
  244. {
  245. m_nRaidoBtnChoose = nResult;
  246. }
  247. void MDialog::setDlgShowSymbol(bool bIsShow)
  248. {
  249. m_bIsExec = bIsShow;
  250. }
  251. void MDialog::onTitleCloseBtnClicked(bool)
  252. {
  253. m_nRaidoBtnChoose = -1;
  254. this->close();
  255. }
  256. //************************************
  257. // Method: onForceCloseBtnClicked
  258. // Description:用户点击强制退出按钮的响应函数
  259. // Author:xxx
  260. // Date:2020/10/26
  261. // Returns: void
  262. // Parameter: bool
  263. //************************************
  264. void MDialog::onForceCloseBtnClicked(bool)
  265. {
  266. m_bIsClickedForceQuitBtn = true;
  267. if (m_bNeedShowSecureTip)
  268. {
  269. m_pListWidgetLayout = new QHBoxLayout(this);
  270. m_pListWidget = new QListWidget(this);
  271. m_pButtonGroup = new QButtonGroup(this);
  272. m_pButtonHLayout = new QHBoxLayout(this);
  273. m_pListWidget->setSpacing(3);
  274. setListStyleSheet(m_pListWidget);
  275. QListWidgetItem* pChooseItem1 = new QListWidgetItem(m_pListWidget);
  276. pChooseItem1->setSizeHint(QSize(20, 38));
  277. QListWidgetItem* pChooseItem2 = new QListWidgetItem(m_pListWidget);
  278. pChooseItem2->setSizeHint(QSize(20, 38));
  279. m_pListWidget->addItem(pChooseItem1);
  280. m_pListWidget->addItem(pChooseItem2);
  281. m_pListWidget->setItemWidget(pChooseItem1, createListItemWidget(QString::fromLocal8Bit("清理本地的缓存文件,以保证文档的安全"), ""));
  282. m_pListWidget->setItemWidget(pChooseItem2, createListItemWidget(QString::fromLocal8Bit("保留数据以供下次使用"), ""));
  283. m_pListWidget->item(0)->setSizeHint(QSize(400 * m_dpi, 38 * m_dpi));
  284. m_pListWidget->item(1)->setSizeHint(QSize(400 * m_dpi, 38 * m_dpi));
  285. m_pSureQuitBtn = new QPushButton(this);
  286. m_pSureQuitBtn->setText(QString::fromLocal8Bit("确认退出"));
  287. m_pSureQuitBtn->setMinimumHeight(30 * m_dpi);
  288. setButtonStyleSheet(m_pSureQuitBtn);
  289. m_pButtonHLayout->addStretch(1);
  290. m_pButtonHLayout->addWidget(m_pSureQuitBtn, 1);
  291. m_pButtonHLayout->addStretch(1);
  292. m_pListWidgetLayout->addWidget(m_pListWidget);
  293. m_pQuitLabel->hide();
  294. m_pForceCloseBtn->hide();
  295. m_pCancelBtn->hide();
  296. m_pToolTipPicture->hide();
  297. QLabel* pSuggestionLabel = new QLabel(this);
  298. pSuggestionLabel->setText(QString::fromLocal8Bit("即将退出账号,您需要:"));
  299. QFont ft(QString::fromLocal8Bit("微软雅黑"), 12, 0);
  300. pSuggestionLabel->setFont(ft);
  301. QFont ftTtitle(QString::fromLocal8Bit("微软雅黑"), 15, 0);
  302. m_pMainTitleLabel->setText(QString::fromLocal8Bit("安全提示"));
  303. m_pMainTitleLabel->setAlignment(Qt::AlignHCenter);
  304. m_pMainTitleLabel->setFont(ftTtitle);
  305. QLayoutItem *QuitContentLayoutChild = nullptr;
  306. while ((QuitContentLayoutChild = m_pQuitContentLayout->takeAt(0)) != 0)
  307. {
  308. //setParent为NULL,防止删除之后界面不消失
  309. if (QuitContentLayoutChild->widget())
  310. {
  311. QuitContentLayoutChild->widget()->setParent(NULL);
  312. }
  313. delete QuitContentLayoutChild;
  314. }
  315. QLayoutItem *ForceCloseAndCancelLayoutChild = nullptr;
  316. while ((ForceCloseAndCancelLayoutChild = m_pForceCloseAndCancelLayout->takeAt(0)) != 0)
  317. {
  318. //setParent为NULL,防止删除之后界面不消失
  319. if (ForceCloseAndCancelLayoutChild->widget())
  320. {
  321. ForceCloseAndCancelLayoutChild->widget()->setParent(NULL);
  322. }
  323. delete ForceCloseAndCancelLayoutChild;
  324. }
  325. QVBoxLayout * pSuggestTionLayout = new QVBoxLayout(this);
  326. pSuggestTionLayout->addWidget(pSuggestionLabel, 1);
  327. pSuggestTionLayout->setMargin(1);
  328. m_pMainVLayout->removeItem(m_pForceCloseAndCancelLayout);
  329. m_pMainVLayout->removeItem(m_pQuitContentLayout);
  330. m_pMainVLayout->addLayout(pSuggestTionLayout, 1);
  331. m_pMainVLayout->addLayout(m_pListWidgetLayout, 6);
  332. m_pMainVLayout->addLayout(m_pButtonHLayout, 1);
  333. this->setMinimumSize(QSize(480 * m_dpi, 260 * m_dpi));
  334. connect(m_pListWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(onListWidgetCurrentItemChanged(QListWidgetItem*, QListWidgetItem*)));
  335. connect(m_pSureQuitBtn, SIGNAL(clicked(bool)), this, SLOT(onSureQuitBtnClicked(bool)));
  336. connect(m_pButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(onButtonGroupClicked(int)));
  337. m_pListWidget->setCurrentItem(m_pListWidget->item(0));
  338. }
  339. else
  340. {
  341. this->close();
  342. }
  343. }
  344. //************************************
  345. // Method: onListWidgetCurrentItemChanged
  346. // Description:listWidget切换每行时响应函数
  347. // Author:xxx
  348. // Date:2020/10/26
  349. // Returns: void
  350. // Parameter: QListWidgetItem * pCurrent
  351. // Parameter: QListWidgetItem * pPrevious
  352. //************************************
  353. void MDialog::onListWidgetCurrentItemChanged(QListWidgetItem* pCurrent, QListWidgetItem* pPrevious)
  354. {
  355. if (pCurrent)
  356. {
  357. m_nRaidoBtnChoose = m_pListWidget->currentRow();
  358. QWidget* pwig = m_pListWidget->itemWidget(pCurrent); // 获取里面的QWidget
  359. if (pwig != NULL)
  360. {
  361. // 方法一 获取所有
  362. QList<QRadioButton*> pRBtnList = pwig->findChildren<QRadioButton*>();
  363. foreach(QRadioButton *pRBtn, pRBtnList)
  364. {
  365. pRBtn->setChecked(true);
  366. }
  367. QList<QLabel*> pLabelList = pwig->findChildren<QLabel*>();
  368. for (int nIndex = 0; nIndex < 1; nIndex++)
  369. {
  370. QPalette palette;
  371. palette.setColor(QPalette::Text, QColor(65, 127, 249));
  372. pLabelList.at(nIndex)->setPalette(palette);
  373. }
  374. }
  375. }
  376. if (pPrevious)
  377. {
  378. QWidget* pwig = m_pListWidget->itemWidget(pPrevious); // 获取里面的QWidget
  379. if (pwig != NULL)
  380. {
  381. // 方法一 获取所有
  382. QList<QRadioButton*> pRBtnList = pwig->findChildren<QRadioButton*>();
  383. foreach(QRadioButton *pRBtn, pRBtnList)
  384. {
  385. pRBtn->setChecked(false);
  386. }
  387. QList<QLabel*> pLabelList = pwig->findChildren<QLabel*>();
  388. for (int nIndex = 0; nIndex < 1; nIndex++)
  389. {
  390. QPalette palette;
  391. palette.setColor(QPalette::WindowText, Qt::black);
  392. pLabelList.at(nIndex)->setPalette(palette);
  393. }
  394. }
  395. }
  396. }
  397. void MDialog::onSureQuitBtnClicked(bool)
  398. {
  399. setResult(m_nRaidoBtnChoose);
  400. this->close();
  401. }
  402. void MDialog::onButtonGroupClicked(int nClicked)
  403. {
  404. qDebug() << "onButtonGroupClicked:" << nClicked;
  405. m_pListWidget->setCurrentRow(nClicked);
  406. }
  407. bool MDialog::eventFilter(QObject *obj, QEvent *event)
  408. {
  409. return QObject::eventFilter(obj, event);
  410. }
  411. void MDialog::paintEvent(QPaintEvent *event)
  412. {
  413. QPainter painter(this);
  414. painter.setRenderHint(QPainter::Antialiasing, true);
  415. painter.fillRect(QRect(SHADOW_WIDTH, SHADOW_WIDTH, this->width() - 2 * SHADOW_WIDTH, this->height() - 2 * SHADOW_WIDTH), QBrush(Qt::white));
  416. QColor color(50, 50, 50, 30);
  417. for (int i = 0; i < SHADOW_WIDTH; i++)
  418. {
  419. color.setAlpha(120 - qSqrt(i) * 40);
  420. painter.setPen(color);
  421. //方角阴影边框;
  422. painter.drawRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2);
  423. //圆角阴影边框;
  424. //painter.drawRoundedRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2, 3, 3);
  425. }
  426. }
  427. void MDialog::mousePressEvent(QMouseEvent *event)
  428. {
  429. if (event->button() == Qt::LeftButton)
  430. {
  431. //只在frameTopTitle区域可以拖动
  432. if (m_pMainTitleLabel->geometry().contains(this->mapFromGlobal(QCursor::pos())))
  433. {
  434. //move 用的是 globalPos, 所以,需要要取出本窗口的(0,0)对应的全局坐标
  435. //pos()返回的是父窗口中位置,不能直接用
  436. ptStartPos_ = event->globalPos() - mapToGlobal(QPoint(0, 0));
  437. bKeyMovePressed_ = true;
  438. }
  439. }
  440. event->accept();
  441. }
  442. void MDialog::mouseMoveEvent(QMouseEvent *event)
  443. {
  444. if (bKeyMovePressed_)
  445. this->move(event->globalPos() - this->ptStartPos_);
  446. event->accept();
  447. }
  448. void MDialog::mouseReleaseEvent(QMouseEvent *event)
  449. {
  450. bKeyMovePressed_ = false;
  451. event->accept();
  452. }
  453. SecureTipDialog::SecureTipDialog(QWidget *parent /*= Q_NULLPTR*/)
  454. {
  455. m_nRaidoBtnChoose = -1;
  456. m_nButtonIndex = 0;
  457. m_dDpi = 1;
  458. _isleftpressed = false;
  459. _curpos = 0;//标记鼠标左击时的位置
  460. m_pMainTitleLabel = new QLabel(this);
  461. m_pMainTitleLabel->setText(QString::fromLocal8Bit("退出账号"));
  462. this->setMouseTracking(true);
  463. Qt::WindowFlags flags = Qt::Dialog;
  464. flags |= Qt::FramelessWindowHint;
  465. flags |= Qt::Tool; //程序不在任务栏显示
  466. flags |= Qt::WindowStaysOnTopHint; //置顶显示
  467. setWindowFlags(flags);
  468. setAttribute(Qt::WA_TranslucentBackground); //透明化
  469. m_pMainVLayout = new QVBoxLayout(this);
  470. m_pListWidgetLayout = new QHBoxLayout(this);
  471. m_pButtonHLayout = new QHBoxLayout(this);
  472. m_pQuitContentLayout = new QHBoxLayout(this);
  473. m_pListWidget = new QListWidget(this);
  474. m_pListWidget->setSpacing(3);
  475. setListStyleSheet(m_pListWidget);
  476. QFont font(QString::fromLocal8Bit("微软雅黑"), 13, 6);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75
  477. m_pMainTitleLabel->setFont(font);
  478. QPalette pa;
  479. pa.setColor(QPalette::WindowText, Qt::black);
  480. m_pMainTitleLabel->setPalette(pa);
  481. m_pButtonGroup = new QButtonGroup(this);
  482. m_pTitleCloseBtn = new QPushButton(this);
  483. m_pTitleCloseBtn->installEventFilter(this);
  484. m_pTitleCloseBtn->setFixedWidth(20);
  485. m_pTitleCloseBtn->setFixedHeight(20);
  486. m_pTitleCloseBtn->setIconSize(QSize(16, 16));
  487. m_pTitleCloseBtn->setStyleSheet("QPushButton{ border-image:url(':/p2.png');}/*QPushButton:hover{ border-image:url(':/window_close_p.png');\
  488. 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));\
  489. }QPushButton{border-radius:0px;color: #F0F0F0;background-color:rgba(0, 0, 0, 0);border-style:none;}*/");
  490. m_pTitleCloseBtn->setToolTip(QString::fromLocal8Bit("关闭"));
  491. QHBoxLayout* pTitleHLayout = new QHBoxLayout(this);
  492. pTitleHLayout->addStretch(4);
  493. pTitleHLayout->addWidget(m_pMainTitleLabel, 5);
  494. pTitleHLayout->addStretch(4);
  495. pTitleHLayout->addWidget(m_pTitleCloseBtn, 1);
  496. //pTitleHLayout->setMargin(SHADOW_WIDTH + 8);
  497. m_pMainVLayout->addLayout(pTitleHLayout, 1);
  498. QLabel *blueLine = new QLabel(this);
  499. blueLine->setMaximumHeight(1);
  500. blueLine->setStyleSheet("background-color:rgb(227, 227, 227);");
  501. QHBoxLayout *blueLineLayout = new QHBoxLayout(this);
  502. blueLineLayout->addWidget(blueLine);
  503. m_pMainVLayout->addLayout(blueLineLayout, 1);
  504. QListWidgetItem* pChooseItem1 = new QListWidgetItem(m_pListWidget);
  505. pChooseItem1->setSizeHint(QSize(20, 38));
  506. QListWidgetItem* pChooseItem2 = new QListWidgetItem(m_pListWidget);
  507. pChooseItem2->setSizeHint(QSize(20, 38));
  508. m_pListWidget->addItem(pChooseItem1);
  509. m_pListWidget->addItem(pChooseItem2);
  510. m_pListWidget->setItemWidget(pChooseItem1, createListItemWidget(QString::fromLocal8Bit("清理本地的缓存文件,以保证文档的安全"), ""));
  511. m_pListWidget->setItemWidget(pChooseItem2, createListItemWidget(QString::fromLocal8Bit("保留数据以供下次使用"), ""));
  512. m_pListWidget->item(0)->setSizeHint(QSize(400 * m_dDpi, 38 * m_dDpi));
  513. m_pListWidget->item(1)->setSizeHint(QSize(400 * m_dDpi, 38 * m_dDpi));
  514. m_pListWidget->setMinimumHeight(80 * m_dDpi);
  515. m_pListWidget->setMaximumHeight(97 * m_dDpi);
  516. m_pSureQuitBtn = new QPushButton(this);
  517. m_pSureQuitBtn->setText(QString::fromLocal8Bit("确认退出"));
  518. m_pSureQuitBtn->setMinimumHeight(30 * m_dDpi);
  519. m_pSureCancelBtn = new QPushButton(this);
  520. m_pSureCancelBtn->setText(QString::fromLocal8Bit("取消"));
  521. m_pSureCancelBtn->setMinimumHeight(30 * m_dDpi);
  522. setButtonStyleSheet(m_pSureQuitBtn);
  523. setButtonStyleSheet(m_pSureCancelBtn);
  524. m_pButtonHLayout->addStretch(1);
  525. m_pButtonHLayout->addWidget(m_pSureQuitBtn, 1);
  526. m_pButtonHLayout->addWidget(m_pSureCancelBtn, 1);
  527. m_pButtonHLayout->addStretch(1);
  528. m_pListWidgetLayout->addWidget(m_pListWidget);
  529. this->setStyleSheet("background-color: white");
  530. QLabel* pSuggestionLabel = new QLabel(this);
  531. pSuggestionLabel->setText(QString::fromLocal8Bit("即将退出账号,您需要:"));
  532. QFont ftSu(QString::fromLocal8Bit("微软雅黑"), 11, 0);
  533. pSuggestionLabel->setFont(ftSu);
  534. //pSuggestionLabel->setMaximumHeight(15 * m_dDpi);
  535. QFont ft(QString::fromLocal8Bit("微软雅黑"), 13, 0);
  536. m_pMainTitleLabel->setFont(ft);
  537. m_pMainTitleLabel->setText(QString::fromLocal8Bit("安全提示"));
  538. m_pMainTitleLabel->setAlignment(Qt::AlignHCenter);
  539. m_pQuitContentLayout->addWidget(pSuggestionLabel);
  540. m_pQuitContentLayout->setMargin(1);
  541. m_pMainVLayout->addLayout(m_pQuitContentLayout, 1);
  542. m_pMainVLayout->addLayout(m_pListWidgetLayout, 5);
  543. m_pMainVLayout->addLayout(m_pButtonHLayout, 1);
  544. m_pMainVLayout->setMargin(SHADOW_WIDTH + 8);
  545. this->setMinimumSize(QSize(480 * m_dDpi, 230 * m_dDpi));
  546. this->setLayout(m_pMainVLayout);
  547. connect(m_pTitleCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
  548. connect(m_pListWidget, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(onListWidgetCurrentItemChanged(QListWidgetItem*, QListWidgetItem*)));
  549. connect(m_pSureQuitBtn, SIGNAL(clicked(bool)), this, SLOT(onSureQuitBtnClicked(bool)));
  550. connect(m_pButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(onButtonGroupClicked(int)));
  551. connect(m_pSureCancelBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
  552. onListWidgetCurrentItemChanged(m_pListWidget->item(0), NULL);
  553. }
  554. SecureTipDialog::~SecureTipDialog()
  555. {
  556. qDebug() << QString::fromLocal8Bit("用户选择的是:") << getResult();
  557. }
  558. int SecureTipDialog::getResult()
  559. {
  560. return m_nRaidoBtnChoose;
  561. }
  562. void SecureTipDialog::setDpi(double fDpi)
  563. {
  564. m_dDpi = fDpi;
  565. }
  566. void SecureTipDialog::init()
  567. {
  568. m_nRaidoBtnChoose = -1;
  569. m_pSureCancelBtn->show();
  570. onListWidgetCurrentItemChanged(m_pListWidget->item(0), NULL);
  571. }
  572. QWidget* SecureTipDialog::createListItemWidget(QString strLabelTitle, QString strLabelContent)
  573. {
  574. QWidget* container = new QWidget(this);
  575. QHBoxLayout* pContainerLayout = new QHBoxLayout(container);
  576. QVBoxLayout* pLabelLayout = new QVBoxLayout(container);
  577. QHBoxLayout* pButtonLayout = new QHBoxLayout(container);
  578. QRadioButton* pRBChoose = new QRadioButton(container);
  579. m_pButtonGroup->addButton(pRBChoose, m_nButtonIndex);
  580. pRBChoose->setIconSize(QSize(30 * m_dDpi, 30 * m_dDpi));
  581. QLabel* pLabelTitle = new QLabel(container);
  582. pLabelTitle->setText(strLabelTitle);
  583. QFont ft(QString::fromLocal8Bit("微软雅黑"), 11, 3);
  584. QPalette pa;
  585. pa.setColor(QPalette::WindowText, Qt::black);
  586. pLabelTitle->setPalette(pa);
  587. pLabelTitle->setFont(ft);
  588. QPalette pa1;
  589. pa1.setColor(QPalette::Text, QColor(170, 170, 170));
  590. pLabelLayout->addWidget(pLabelTitle);
  591. pButtonLayout->addWidget(pRBChoose);
  592. pContainerLayout->addLayout(pButtonLayout, 1);
  593. pContainerLayout->addLayout(pLabelLayout, 5);
  594. container->setLayout(pContainerLayout);
  595. m_nButtonIndex++;
  596. return container;
  597. }
  598. void SecureTipDialog::setButtonStyleSheet(QPushButton*& pBtn)
  599. {
  600. QString strStyle = "";
  601. if (pBtn->text() == QString::fromLocal8Bit("是") || pBtn->text() == QString::fromLocal8Bit("确认退出"))
  602. {
  603. 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);}";
  604. }
  605. else
  606. {
  607. 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);}";
  608. }
  609. strStyle += "/*按钮停留态*/\
  610. QPushButton:hover\
  611. {\
  612. /*背景颜色*/\
  613. /*background-color:rgb(65, 127, 249);*/\
  614. /*color:white;*/\
  615. }\
  616. /*按钮按下态*/\
  617. QPushButton:pressed\
  618. {\
  619. /*背景颜色*/\
  620. background-color:rgb(59, 119, 229);\
  621. /*左内边距为3像素,让按下时字向右移动3像素*/\
  622. padding-left:3px;\
  623. /*上内边距为3像素,让按下时字向下移动3像素*/\
  624. padding-top:3px;\
  625. }";
  626. pBtn->setStyleSheet(strStyle);
  627. }
  628. void SecureTipDialog::setListStyleSheet(QListWidget*& listWidget)
  629. {
  630. QString strStyleSheet = "QListWidget{\
  631. border:1px solid rgb(232, 232, 232); /*边界线:宽度、颜色 */\
  632. /*background:gray;*/ /*表格背景色*/\
  633. color:black; /*前景色:文字颜色*/\
  634. /*margin:5px,5px,0px,50px;*/ /*上、下、左、右,间距*/}\
  635. QListWidget::item{\
  636. /*height:40px;*/\
  637. padding-top:2px;\
  638. padding-bottom:2px;\
  639. border-bottom: 1px solid rgb(234, 234, 234);\
  640. }";
  641. listWidget->setStyleSheet(strStyleSheet);
  642. }
  643. void SecureTipDialog::setResult(int nResult)
  644. {
  645. m_nRaidoBtnChoose = nResult;
  646. }
  647. void SecureTipDialog::onTitleCloseBtnClicked(bool)
  648. {
  649. m_nRaidoBtnChoose = -1;
  650. this->close();
  651. }
  652. void SecureTipDialog::onListWidgetCurrentItemChanged(QListWidgetItem* pCurrent, QListWidgetItem* pPrevious)
  653. {
  654. if (pCurrent)
  655. {
  656. m_nRaidoBtnChoose = m_pListWidget->currentRow();
  657. QWidget* pwig = m_pListWidget->itemWidget(pCurrent); // 获取里面的QWidget
  658. if (pwig != NULL)
  659. {
  660. // 方法一 获取所有
  661. QList<QRadioButton*> pRBtnList = pwig->findChildren<QRadioButton*>();
  662. foreach(QRadioButton *pRBtn, pRBtnList)
  663. {
  664. pRBtn->setChecked(true);
  665. }
  666. QList<QLabel*> pLabelList = pwig->findChildren<QLabel*>();
  667. for (int nIndex = 0; nIndex < 1; nIndex++)
  668. {
  669. QPalette palette;
  670. palette.setColor(QPalette::Text, QColor(65, 127, 249));
  671. pLabelList.at(nIndex)->setPalette(palette);
  672. }
  673. }
  674. }
  675. if (pPrevious)
  676. {
  677. QWidget* pwig = m_pListWidget->itemWidget(pPrevious); // 获取里面的QWidget
  678. if (pwig != NULL)
  679. {
  680. // 方法一 获取所有
  681. QList<QRadioButton*> pRBtnList = pwig->findChildren<QRadioButton*>();
  682. foreach(QRadioButton *pRBtn, pRBtnList)
  683. {
  684. pRBtn->setChecked(false);
  685. }
  686. QList<QLabel*> pLabelList = pwig->findChildren<QLabel*>();
  687. for (int nIndex = 0; nIndex < 1; nIndex++)
  688. {
  689. QPalette palette;
  690. palette.setColor(QPalette::WindowText, Qt::black);
  691. pLabelList.at(nIndex)->setPalette(palette);
  692. }
  693. }
  694. }
  695. }
  696. void SecureTipDialog::onSureQuitBtnClicked(bool)
  697. {
  698. setResult(m_nRaidoBtnChoose);
  699. this->close();
  700. }
  701. void SecureTipDialog::onButtonGroupClicked(int nClicked)
  702. {
  703. qDebug() << "onButtonGroupClicked:" << nClicked;
  704. m_pListWidget->setCurrentRow(nClicked);
  705. }
  706. bool SecureTipDialog::eventFilter(QObject *obj, QEvent *event)
  707. {
  708. return QObject::eventFilter(obj, event);
  709. }
  710. void SecureTipDialog::mousePressEvent(QMouseEvent *event)
  711. {
  712. if (event->button() == Qt::LeftButton)
  713. {
  714. this->_isleftpressed = true;
  715. QPoint temp = event->globalPos();
  716. _plast = temp;
  717. _curpos = countFlag(event->pos(), countRow(event->pos()));
  718. event->ignore();
  719. }
  720. }
  721. void SecureTipDialog::mouseMoveEvent(QMouseEvent *event)
  722. {
  723. int poss = countFlag(event->pos(), countRow(event->pos()));
  724. if (_isleftpressed)//是否左击
  725. {
  726. QPoint ptemp = event->globalPos();
  727. ptemp = ptemp - _plast;
  728. if (_curpos == 22)//移动窗口
  729. {
  730. ptemp = ptemp + pos();
  731. move(ptemp);
  732. }
  733. _plast = event->globalPos();//更新位置
  734. }
  735. event->ignore();
  736. }
  737. void SecureTipDialog::mouseReleaseEvent(QMouseEvent *event)
  738. {
  739. if (_isleftpressed)
  740. _isleftpressed = false;
  741. setCursor(Qt::ArrowCursor);
  742. event->ignore();
  743. }
  744. int SecureTipDialog::countFlag(QPoint p, int row)
  745. {
  746. if (p.y() < MARGIN)
  747. return 10 + row;
  748. else if (p.y() > this->height() - MARGIN)
  749. return 30 + row;
  750. else
  751. return 20 + row;
  752. }
  753. int SecureTipDialog::countRow(QPoint p)
  754. {
  755. return (p.x() < MARGIN) ? 1 : (p.x() > (this->width() - MARGIN) ? 3 : 2);
  756. }
  757. void SecureTipDialog::paintEvent(QPaintEvent *event)
  758. {
  759. QPainter painter(this);
  760. painter.setRenderHint(QPainter::Antialiasing, true);
  761. painter.fillRect(QRect(SHADOW_WIDTH, SHADOW_WIDTH, this->width() - 2 * SHADOW_WIDTH, this->height() - 2 * SHADOW_WIDTH), QBrush(Qt::white));
  762. QColor color(50, 50, 50, 30);
  763. for (int i = 0; i < SHADOW_WIDTH; i++)
  764. {
  765. color.setAlpha(120 - qSqrt(i) * 40);
  766. painter.setPen(color);
  767. //方角阴影边框;
  768. painter.drawRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2);
  769. //圆角阴影边框;
  770. //painter.drawRoundedRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2, 3, 3);
  771. }
  772. }
  773. MessageDialog::MessageDialog(QWidget *parent /*= Q_NULLPTR*/)
  774. : QDialog(parent)
  775. {
  776. m_nResult = -1;
  777. _isleftpressed = false;
  778. _curpos = 0;//标记鼠标左击时的位置
  779. m_dDpi = 1;
  780. m_pMainTitleLabel = new QLabel(this);
  781. m_pMainTitleLabel->setText(QString::fromLocal8Bit("升级提示"));
  782. this->setMouseTracking(true);
  783. Qt::WindowFlags flags = Qt::Dialog;
  784. flags |= Qt::FramelessWindowHint;
  785. flags |= Qt::Tool; //程序不在任务栏显示
  786. flags |= Qt::WindowStaysOnTopHint; //置顶显示
  787. flags |= Qt::Popup;
  788. setWindowFlags(flags);
  789. setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
  790. setAttribute(Qt::WA_TranslucentBackground); //透明化
  791. m_pMainVLayout = new QVBoxLayout(this);
  792. QVBoxLayout* pQuitContentLayout = new QVBoxLayout(this);
  793. QHBoxLayout* pTitleHLayout = new QHBoxLayout(this);
  794. QFont font(QString::fromLocal8Bit("微软雅黑"), 13, 6);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75
  795. QPalette pa;
  796. pa.setColor(QPalette::WindowText, Qt::black);
  797. m_pMainTitleLabel->setFont(font);
  798. m_pMainTitleLabel->setPalette(pa);
  799. m_pTitleCloseBtn = new QPushButton(this);
  800. m_pTitleCloseBtn->installEventFilter(this);
  801. m_pTitleCloseBtn->setMinimumWidth(18);
  802. m_pTitleCloseBtn->setMinimumHeight(18);
  803. m_pTitleCloseBtn->setIconSize(QSize(16, 16));
  804. /*m_pTitleCloseBtn->setStyleSheet("QPushButton{ border-image:url('../dialog1/window_close_n.png');}QPushButton:hover{ border-image:url('../dialog1/window_close_p.png');\
  805. 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));\
  806. }QPushButton{border-radius:0px;color: #F0F0F0;background-color:rgba(0, 0, 0, 0);border-style:none;}");*/
  807. m_pTitleCloseBtn->setStyleSheet("QPushButton{border-image:url('../dialog1/p2.png');}");
  808. pTitleHLayout->addWidget(m_pMainTitleLabel, 14);
  809. pTitleHLayout->addStretch(10);
  810. pTitleHLayout->addWidget(m_pTitleCloseBtn, 1);
  811. m_pMainVLayout->addLayout(pTitleHLayout, 1);
  812. QVBoxLayout* blueLayout = new QVBoxLayout(this);
  813. QLabel *blueLine = new QLabel(this);
  814. blueLine->setMaximumHeight(1);
  815. blueLine->setStyleSheet("background-color:rgb(227, 227, 227);");
  816. blueLayout->addWidget(blueLine);
  817. m_pMainVLayout->addLayout(blueLayout, 1);
  818. m_pQuitLabel = new QLabel(this);
  819. m_pQuitLabel->setText(QString::fromLocal8Bit("检测到软件有新版,是否更新到最新版以获得更优的使用体验!"));
  820. QFont fontQuitLabel(QString::fromLocal8Bit("微软雅黑"), 11, 10);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75
  821. m_pQuitLabel->setFont(fontQuitLabel);
  822. QPalette paQuitLabel;
  823. paQuitLabel.setColor(QPalette::Text, QColor(66, 66, 66, 255));
  824. m_pQuitLabel->setPalette(paQuitLabel);
  825. m_pForceCloseBtn = new QPushButton(this);
  826. m_pForceCloseBtn->setText(QString::fromLocal8Bit("立即升级"));
  827. m_pForceCloseBtn->setMinimumSize(QSize(80 * m_dDpi, 30 * m_dDpi));
  828. m_pForceCloseBtn->setChecked(true);
  829. m_pCancelBtn = new QPushButton(this);
  830. m_pCancelBtn->setText(QString::fromLocal8Bit("下次再说"));
  831. m_pCancelBtn->setMinimumSize(QSize(80 * m_dDpi, 30 * m_dDpi));
  832. m_pForceCloseAndCancelLayout = new QHBoxLayout(this);
  833. setButtonStyleSheet(m_pForceCloseBtn, true);
  834. setButtonStyleSheet(m_pCancelBtn, false);
  835. m_pForceCloseAndCancelLayout->addStretch(13);
  836. m_pForceCloseAndCancelLayout->addWidget(m_pForceCloseBtn);
  837. m_pForceCloseAndCancelLayout->addWidget(m_pCancelBtn);
  838. m_pForceCloseAndCancelLayout->addStretch(1);
  839. pQuitContentLayout->addWidget(m_pQuitLabel);
  840. m_pMainVLayout->addLayout(pQuitContentLayout, 3);
  841. m_pMainVLayout->addLayout(m_pForceCloseAndCancelLayout, 1);
  842. m_pMainVLayout->addStretch(1);
  843. this->setMinimumSize(QSize(430 * m_dDpi, 160 * m_dDpi));
  844. this->setStyleSheet("background-color: white");
  845. m_pMainVLayout->setMargin(SHADOW_WIDTH + 8);
  846. this->setLayout(m_pMainVLayout);
  847. connect(m_pTitleCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
  848. connect(m_pForceCloseBtn, SIGNAL(clicked(bool)), this, SLOT(onForceCloseBtnClicked(bool)));
  849. connect(m_pCancelBtn, SIGNAL(clicked(bool)), this, SLOT(onTitleCloseBtnClicked(bool)));
  850. }
  851. MessageDialog::~MessageDialog()
  852. {
  853. qDebug() << "user select:" << getResult();
  854. }
  855. int MessageDialog::getResult()
  856. {
  857. return m_nResult;
  858. }
  859. void MessageDialog::setTitleText(QString strTitle)
  860. {
  861. m_pMainTitleLabel->setText(strTitle);
  862. }
  863. void MessageDialog::setContentText(QString strContent)
  864. {
  865. m_pQuitLabel->setText(strContent);
  866. }
  867. void MessageDialog::setBtnOneText(QString strBtn1text)
  868. {
  869. m_pForceCloseBtn->setText(strBtn1text);
  870. }
  871. void MessageDialog::setBtnTwoText(QString strBtn2text)
  872. {
  873. m_pCancelBtn->setText(strBtn2text);
  874. }
  875. void MessageDialog::setButtonStyleSheet(QPushButton*& pBtn, bool bBlue)
  876. {
  877. QString strStyle = "";
  878. if (bBlue)
  879. {
  880. 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);}";
  881. }
  882. else
  883. {
  884. 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);}";
  885. }
  886. strStyle += "/*按钮停留态*/\
  887. QPushButton:hover{\
  888. /*背景颜色*/\
  889. /*background-color:rgb(65, 127, 249);*/\
  890. /*color:white;*/\
  891. }\
  892. /*按钮按下态*/\
  893. QPushButton:pressed {\
  894. /*背景颜色*/\
  895. background-color:rgb(59, 119, 229);\
  896. /*左内边距为3像素,让按下时字向右移动3像素*/\
  897. padding-left:3px;\
  898. /*上内边距为3像素,让按下时字向下移动3像素*/\
  899. padding-top:3px;\
  900. }";
  901. pBtn->setStyleSheet(strStyle);
  902. }
  903. void MessageDialog::setResult(int nResult)
  904. {
  905. m_nResult = nResult;
  906. }
  907. void MessageDialog::onTitleCloseBtnClicked(bool)
  908. {
  909. setResult(0);
  910. this->close();
  911. }
  912. void MessageDialog::onForceCloseBtnClicked(bool)
  913. {
  914. setResult(1);
  915. this->close();
  916. }
  917. bool MessageDialog::eventFilter(QObject *obj, QEvent *event)
  918. {
  919. return QObject::eventFilter(obj, event);
  920. }
  921. void MessageDialog::mousePressEvent(QMouseEvent *event)
  922. {
  923. if (event->button() == Qt::LeftButton)
  924. {
  925. this->_isleftpressed = true;
  926. QPoint temp = event->globalPos();
  927. _plast = temp;
  928. _curpos = countFlag(event->pos(), countRow(event->pos()));
  929. event->ignore();
  930. }
  931. }
  932. void MessageDialog::mouseMoveEvent(QMouseEvent *event)
  933. {
  934. int poss = countFlag(event->pos(), countRow(event->pos()));
  935. if (_isleftpressed)//是否左击
  936. {
  937. QPoint ptemp = event->globalPos();
  938. ptemp = ptemp - _plast;
  939. if (_curpos == 22)//移动窗口
  940. {
  941. ptemp = ptemp + pos();
  942. move(ptemp);
  943. }
  944. _plast = event->globalPos();//更新位置
  945. }
  946. event->ignore();
  947. }
  948. void MessageDialog::mouseReleaseEvent(QMouseEvent *event)
  949. {
  950. if (_isleftpressed)
  951. _isleftpressed = false;
  952. setCursor(Qt::ArrowCursor);
  953. event->ignore();
  954. }
  955. int MessageDialog::countFlag(QPoint p, int row)
  956. {
  957. if (p.y() < MARGIN)
  958. return 10 + row;
  959. else if (p.y() > this->height() - MARGIN)
  960. return 30 + row;
  961. else
  962. return 20 + row;
  963. }
  964. int MessageDialog::countRow(QPoint p)
  965. {
  966. return (p.x() < MARGIN) ? 1 : (p.x() > (this->width() - MARGIN) ? 3 : 2);
  967. }
  968. void MessageDialog::paintEvent(QPaintEvent *event)
  969. {
  970. QPainter painter(this);
  971. painter.setRenderHint(QPainter::Antialiasing, true);
  972. painter.fillRect(QRect(SHADOW_WIDTH, SHADOW_WIDTH, this->width() - 2 * SHADOW_WIDTH, this->height() - 2 * SHADOW_WIDTH), QBrush(Qt::white));
  973. QColor color(50, 50, 50, 30);
  974. for (int i = 0; i < SHADOW_WIDTH; i++)
  975. {
  976. color.setAlpha(120 - qSqrt(i) * 40);
  977. painter.setPen(color);
  978. //方角阴影边框;
  979. painter.drawRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2);
  980. //圆角阴影边框;
  981. //painter.drawRoundedRect(SHADOW_WIDTH - i, SHADOW_WIDTH - i, this->width() - (SHADOW_WIDTH - i) * 2, this->height() - (SHADOW_WIDTH - i) * 2, 4, 4);
  982. }
  983. }

调用:

main.cpp

  1. #include "mdialog.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. // MDialog w;
  7. // w.show();
  8. MessageDialog dlg;
  9. dlg.exec();
  10. return a.exec();
  11. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/94938
推荐阅读
相关标签