DialogBase.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "DialogBase.h"
  2. #include <QMouseEvent>
  3. #include <QVBoxLayout>
  4. #include <QLabel>
  5. #include <QPushButton>
  6. #include <QBoxLayout>
  7. #include <qboxlayout.h>
  8. #include <qpushbutton.h>
  9. DialogBase::DialogBase(QWidget *parent)
  10. : QDialog(parent)
  11. {
  12. }
  13. DialogBase::~DialogBase()
  14. {
  15. }
  16. /* 设置标题 */
  17. void DialogBase::setTitle(const QString &title, QSize size)
  18. {
  19. }
  20. /* 获取标题 */
  21. QString DialogBase::getTitle() const
  22. {
  23. return m_labelTitle ? m_labelTitle->text() : QString();
  24. }
  25. /* 初始化UI */
  26. void DialogBase::initUI()
  27. {
  28. /*--------------------- 初始化背景容器 -----------------------*/
  29. m_widgetBackground = new QWidget(this);
  30. m_widgetBackground->setObjectName("widget_background");
  31. QVBoxLayout* layout = new QVBoxLayout(this);
  32. /* 设置边距,就是阴影的宽度 */
  33. layout->setContentsMargins(20, 20, 20, 20);
  34. layout->setSpacing(0);
  35. this->setLayout(layout);
  36. layout->addWidget(m_widgetBackground);
  37. /* 创建背景容器内部的布局 */
  38. m_layoutBackground = new QVBoxLayout(m_widgetBackground);
  39. m_layoutBackground->setContentsMargins(0, 0, 0, 0);
  40. m_layoutBackground->setSpacing(0);
  41. m_widgetBackground->setLayout(m_layoutBackground);
  42. /*--------------------- 初始顶栏 -----------------------*/
  43. /* 初始化顶部标题栏,高度56,下面分割线高度1 */
  44. m_widgetTop = new QWidget(this);
  45. m_widgetTop->setObjectName("widget_top");
  46. m_widgetTop->setFixedHeight(56);
  47. m_layoutBackground->addWidget(m_widgetTop);
  48. QHBoxLayout* topHBoxLayout = new QHBoxLayout(m_widgetTop);
  49. m_widgetTop->setLayout(topHBoxLayout);
  50. /* 初始化标题栏 */
  51. m_labelTitle = new QLabel(m_widgetTop);
  52. m_labelTitle->setObjectName("label_title");
  53. m_labelTitle->setText("Dialog Title");
  54. m_labelTitle->setFixedSize(120, 18);
  55. m_labelTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  56. /* 初始化关闭按钮 */
  57. m_pBtn_Close = new QPushButton(m_widgetTop);
  58. m_pBtn_Close->setObjectName("pBtn_Close");
  59. m_pBtn_Close->resize(32, 32);
  60. /* 重新布局顶栏 */
  61. layoutTop();
  62. }
  63. /* 设置top栏的位置布局,标题和关闭按钮的高度起始位置不一样,不方便使用布局,所有手动修改坐标
  64. * 标题位置32,18,高度18
  65. * 关闭按钮右对齐16,y12,大小32x32 */
  66. void DialogBase::layoutTop()
  67. {
  68. m_labelTitle->move(32, 18);
  69. int x = m_widgetTop->width() - m_pBtn_Close->width() - 16;
  70. m_pBtn_Close->move(x, 12);
  71. }
  72. /* 重写鼠标按下事件 */
  73. void DialogBase::mousePressEvent(QMouseEvent *event)
  74. {
  75. m_lastPos = event->globalPos();
  76. event->accept();
  77. }
  78. /* 重写鼠标移动事件 */
  79. void DialogBase::mouseMoveEvent(QMouseEvent *event)
  80. {
  81. auto point = m_widgetTop->mapToGlobal(QPoint(0, 0));
  82. QRect rect(point, m_widgetTop->size());
  83. if(!rect.contains(m_lastPos))
  84. {
  85. event->accept();
  86. return;
  87. }
  88. int dx = event->globalX() - m_lastPos.x();
  89. int dy = event->globalY() - m_lastPos.y();
  90. m_widgetBackground->move(m_widgetBackground->x() + dx, m_widgetBackground->y() + dy);
  91. m_lastPos = event->globalPos();
  92. event->accept();
  93. }
  94. /* 重写鼠标释放事件 */
  95. void DialogBase::mouseReleaseEvent(QMouseEvent *event)
  96. {
  97. event->accept();
  98. }