popMsgBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "popMsgBox.h"
  2. #include "ui_popMsgBox.h"
  3. #include "CAppInfo.h"
  4. #include <QDesktopWidget>
  5. #include <QGraphicsDropShadowEffect>
  6. popMsgBox::popMsgBox(QWidget *parent) :
  7. QDialog(parent),
  8. ui(new Ui::popMsgBox)
  9. {
  10. ui->setupUi(this);
  11. setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  12. setAttribute(Qt::WA_TranslucentBackground);
  13. setStyleSheet(g_appInfoPtr->m_sQssStr);
  14. QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);
  15. effect->setColor(QColor(0, 0, 0, 0.08*255));
  16. effect->setOffset(g_appInfoPtr->m_fRate*2, g_appInfoPtr->m_fRate*2);
  17. effect->setBlurRadius(g_appInfoPtr->m_fRate*12);
  18. setGraphicsEffect(effect);
  19. }
  20. popMsgBox::~popMsgBox()
  21. {
  22. delete ui;
  23. }
  24. void popMsgBox::setBtnText(QString sBtnYesText, QString sBtnNoText)
  25. {
  26. ui->btn_yes->setText(sBtnYesText);
  27. ui->btn_no->setText(sBtnNoText);
  28. }
  29. void popMsgBox::setMsg(QString sMsg, QString sTitle)
  30. {
  31. ui->label_pmb_content->setText(sMsg);
  32. ui->label_pmb_title->setText(sTitle);
  33. }
  34. void popMsgBox::initUI(POP_MSG_BTN btn)
  35. {
  36. int nCW = g_appInfoPtr->m_fRate*352;
  37. QDesktopWidget *dekwiget = QApplication::desktop();
  38. setGeometry((dekwiget->width() - nCW)/2, (dekwiget->height() - g_appInfoPtr->m_fRate*265)/2, nCW, g_appInfoPtr->m_fRate*265);
  39. ui->widget_pmb_bg->setGeometry(g_appInfoPtr->m_fRate*6, g_appInfoPtr->m_fRate*6 , nCW - g_appInfoPtr->m_fRate*12, g_appInfoPtr->m_fRate*253);
  40. ui->label_pmb_icon->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
  41. g_appInfoPtr->m_fRate*40, g_appInfoPtr->m_fRate*40);
  42. ui->label_pmb_title->adjustSize();
  43. ui->label_pmb_title->setGeometry(ui->label_pmb_icon->x() + ui->label_pmb_icon->width() + g_appInfoPtr->m_fRate*16, g_appInfoPtr->m_fRate*30,
  44. ui->label_pmb_title->width(), ui->label_pmb_title->height());
  45. ui->label_pmb_content->setFixedWidth(nCW - ui->label_pmb_icon->x() - ui->label_pmb_icon->width() - g_appInfoPtr->m_fRate*16*2 - g_appInfoPtr->m_fRate*12);
  46. ui->label_pmb_content->adjustSize();
  47. ui->label_pmb_content->setGeometry(ui->label_pmb_icon->x() + ui->label_pmb_icon->width() + g_appInfoPtr->m_fRate*16,
  48. ui->label_pmb_title->y() + ui->label_pmb_title->height() + g_appInfoPtr->m_fRate*8,
  49. ui->label_pmb_content->width(), ui->label_pmb_content->height());
  50. if (btn == POP_MSG_BTN::pmb_yes)
  51. {
  52. ui->btn_no->setVisible(false);
  53. }
  54. ui->btn_yes->setGeometry(ui->widget_pmb_bg->width() - g_appInfoPtr->m_fRate*(20 + 80),
  55. ui->widget_pmb_bg->height() - g_appInfoPtr->m_fRate*(20 + 30),
  56. g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*30);
  57. ui->btn_no->setGeometry(ui->btn_yes->x() - g_appInfoPtr->m_fRate*15 - ui->btn_yes->width(),
  58. ui->btn_yes->y(), ui->btn_yes->width(), ui->btn_yes->height());
  59. }
  60. int popMsg(QString sMsg, QString sTitle, QWidget *parent, QString sBtnYesText, QString sBtnNoText)
  61. {
  62. popMsgBox msgDlg(parent);
  63. msgDlg.setMsg(sMsg, sTitle);
  64. msgDlg.initUI(POP_MSG_BTN::pmb_yse_no);
  65. msgDlg.setBtnText(sBtnYesText, sBtnNoText);
  66. return msgDlg.exec();
  67. }
  68. int popMsg(QString sMsg, QString sTitle, QWidget *parent, POP_MSG_BTN btn, int nStayTime)
  69. {
  70. popMsgBox msgDlg(parent);
  71. msgDlg.setMsg(sMsg, sTitle);
  72. msgDlg.initUI(btn);
  73. if (nStayTime != 0)
  74. {
  75. msgDlg.setStayTime(nStayTime);
  76. }
  77. return msgDlg.exec();
  78. }
  79. void popMsgBox::setStayTime(int nStayTime)
  80. {
  81. m_timePtr = std::make_shared<QTimer>();
  82. m_timePtr->setInterval(nStayTime * 1000);
  83. connect(m_timePtr.get(), &QTimer::timeout, this, [&](){
  84. accept();
  85. });
  86. m_timePtr->start();
  87. }
  88. void popMsgBox::on_btn_yes_clicked()
  89. {
  90. if (m_timePtr != nullptr)
  91. {
  92. m_timePtr->stop();
  93. }
  94. accept();
  95. }
  96. void popMsgBox::on_btn_no_clicked()
  97. {
  98. if (m_timePtr != nullptr)
  99. {
  100. m_timePtr->stop();
  101. }
  102. reject();
  103. }