popMsgBox.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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*203)/2, nCW, g_appInfoPtr->m_fRate*203);
  28. ui->widget_pmb_bg->setGeometry(0, 0 , nCW, g_appInfoPtr->m_fRate*203);
  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->setGeometry(ui->label_pmb_icon->x() + ui->label_pmb_icon->width() + g_appInfoPtr->m_fRate*16,
  35. ui->label_pmb_title->y() + ui->label_pmb_title->height() + g_appInfoPtr->m_fRate*8,
  36. 224, 80);
  37. if (btn == POP_MSG_BTN::pmb_yes)
  38. {
  39. ui->btn_no->setVisible(false);
  40. }
  41. ui->btn_yes->setGeometry(ui->widget_pmb_bg->width() - g_appInfoPtr->m_fRate*(20 + 80),
  42. ui->widget_pmb_bg->height() - g_appInfoPtr->m_fRate*(20 + 30),
  43. g_appInfoPtr->m_fRate*80, g_appInfoPtr->m_fRate*30);
  44. ui->btn_no->setGeometry(ui->btn_yes->x() - g_appInfoPtr->m_fRate*15 - ui->btn_yes->width(),
  45. ui->btn_yes->y(), ui->btn_yes->width(), ui->btn_yes->height());
  46. }
  47. int popMsg(QString sMsg, QString sTitle, QWidget *parent, POP_MSG_BTN btn, int nStayTime)
  48. {
  49. popMsgBox msgDlg(parent);
  50. msgDlg.setMsg(sMsg, sTitle);
  51. msgDlg.initUI(btn);
  52. if (nStayTime != 0)
  53. {
  54. msgDlg.setStayTime(nStayTime);
  55. }
  56. return msgDlg.exec();
  57. }
  58. void popMsgBox::setStayTime(int nStayTime)
  59. {
  60. m_timePtr = std::make_shared<QTimer>();
  61. m_timePtr->setInterval(nStayTime * 1000);
  62. connect(m_timePtr.get(), &QTimer::timeout, this, [&](){
  63. accept();
  64. });
  65. m_timePtr->start();
  66. }
  67. void popMsgBox::on_btn_yes_clicked()
  68. {
  69. if (m_timePtr != nullptr)
  70. {
  71. m_timePtr->stop();
  72. }
  73. accept();
  74. }
  75. void popMsgBox::on_btn_no_clicked()
  76. {
  77. if (m_timePtr != nullptr)
  78. {
  79. m_timePtr->stop();
  80. }
  81. reject();
  82. }