popMsgBox.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "popMsgBox.h"
  2. #include "ui_popMsgBox.h"
  3. #include "CAppInfo.h"
  4. #include <QDesktopWidget>
  5. popMsgBox::popMsgBox(QWidget *parent) :
  6. QDialog(parent),
  7. ui(new Ui::popMsgBox)
  8. {
  9. ui->setupUi(this);
  10. setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  11. setAttribute(Qt::WA_TranslucentBackground); //小部件应该具有半透明的背景
  12. setStyleSheet(g_appInfoPtr->m_sQssStr);
  13. }
  14. popMsgBox::~popMsgBox()
  15. {
  16. delete ui;
  17. }
  18. void popMsgBox::setMsg(QString sMsg, QString sTitle)
  19. {
  20. ui->label_pmb_content->setText(sMsg);
  21. ui->label_pmb_title->setText(sTitle);
  22. }
  23. void popMsgBox::initUI(POP_MSG_BTN btn)
  24. {
  25. int nCW = g_appInfoPtr->m_fRate*340;
  26. QDesktopWidget *dekwiget = QApplication::desktop();
  27. setGeometry((dekwiget->width() - nCW)/2, (dekwiget->height() - g_appInfoPtr->m_fRate*253)/2, nCW, g_appInfoPtr->m_fRate*253);
  28. ui->widget_pmb_bg->setGeometry(0, 0 , nCW, g_appInfoPtr->m_fRate*253);
  29. ui->label_pmb_icon->setGeometry(g_appInfoPtr->m_fRate*30, g_appInfoPtr->m_fRate*30,
  30. g_appInfoPtr->m_fRate*40, g_appInfoPtr->m_fRate*40);
  31. ui->label_pmb_title->adjustSize();
  32. 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,
  33. ui->label_pmb_title->width(), ui->label_pmb_title->height());
  34. ui->label_pmb_content->setFixedWidth(nCW - ui->label_pmb_icon->x() - ui->label_pmb_icon->width() - g_appInfoPtr->m_fRate*16*2);\
  35. ui->label_pmb_content->adjustSize();
  36. ui->label_pmb_content->setGeometry(ui->label_pmb_icon->x() + ui->label_pmb_icon->width() + g_appInfoPtr->m_fRate*16,
  37. ui->label_pmb_title->y() + ui->label_pmb_title->height() + g_appInfoPtr->m_fRate*8,
  38. ui->label_pmb_content->width(), ui->label_pmb_content->height());
  39. if (btn == POP_MSG_BTN::pmb_yes)
  40. {
  41. ui->btn_no->setVisible(false);
  42. }
  43. ui->btn_yes->setGeometry(ui->widget_pmb_bg->width() - g_appInfoPtr->m_fRate*(20 + 80),
  44. ui->widget_pmb_bg->height() - g_appInfoPtr->m_fRate*(20 + 30),
  45. g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*30);
  46. ui->btn_no->setGeometry(ui->btn_yes->x() - g_appInfoPtr->m_fRate*15 - ui->btn_yes->width(),
  47. ui->btn_yes->y(), ui->btn_yes->width(), ui->btn_yes->height());
  48. }
  49. int popMsg(QString sMsg, QString sTitle, QWidget *parent, POP_MSG_BTN btn, int nStayTime)
  50. {
  51. popMsgBox msgDlg(parent);
  52. msgDlg.setMsg(sMsg, sTitle);
  53. msgDlg.initUI(btn);
  54. if (nStayTime != 0)
  55. {
  56. msgDlg.setStayTime(nStayTime);
  57. }
  58. return msgDlg.exec();
  59. }
  60. void popMsgBox::setStayTime(int nStayTime)
  61. {
  62. m_timePtr = std::make_shared<QTimer>();
  63. m_timePtr->setInterval(nStayTime * 1000);
  64. connect(m_timePtr.get(), &QTimer::timeout, this, [&](){
  65. accept();
  66. });
  67. m_timePtr->start();
  68. }
  69. void popMsgBox::on_btn_yes_clicked()
  70. {
  71. if (m_timePtr != nullptr)
  72. {
  73. m_timePtr->stop();
  74. }
  75. accept();
  76. }
  77. void popMsgBox::on_btn_no_clicked()
  78. {
  79. if (m_timePtr != nullptr)
  80. {
  81. m_timePtr->stop();
  82. }
  83. reject();
  84. }