123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include "DialogBase.h"
- #include <QMouseEvent>
- #include <QVBoxLayout>
- #include <QLabel>
- #include <QPushButton>
- #include <QBoxLayout>
- #include <qboxlayout.h>
- #include <qpushbutton.h>
- DialogBase::DialogBase(QWidget *parent)
- : QDialog(parent)
- {
- }
- DialogBase::~DialogBase()
- {
- }
- /* 设置标题 */
- void DialogBase::setTitle(const QString &title, QSize size)
- {
- }
- /* 获取标题 */
- QString DialogBase::getTitle() const
- {
- return m_labelTitle ? m_labelTitle->text() : QString();
- }
- /* 初始化UI */
- void DialogBase::initUI()
- {
- /*--------------------- 初始化背景容器 -----------------------*/
- m_widgetBackground = new QWidget(this);
- m_widgetBackground->setObjectName("widget_background");
- QVBoxLayout* layout = new QVBoxLayout(this);
- /* 设置边距,就是阴影的宽度 */
- layout->setContentsMargins(20, 20, 20, 20);
- layout->setSpacing(0);
- this->setLayout(layout);
- layout->addWidget(m_widgetBackground);
- /* 创建背景容器内部的布局 */
- m_layoutBackground = new QVBoxLayout(m_widgetBackground);
- m_layoutBackground->setContentsMargins(0, 0, 0, 0);
- m_layoutBackground->setSpacing(0);
- m_widgetBackground->setLayout(m_layoutBackground);
- /*--------------------- 初始顶栏 -----------------------*/
- /* 初始化顶部标题栏,高度56,下面分割线高度1 */
- m_widgetTop = new QWidget(this);
- m_widgetTop->setObjectName("widget_top");
- m_widgetTop->setFixedHeight(56);
- m_layoutBackground->addWidget(m_widgetTop);
- QHBoxLayout* topHBoxLayout = new QHBoxLayout(m_widgetTop);
- m_widgetTop->setLayout(topHBoxLayout);
- /* 初始化标题栏 */
- m_labelTitle = new QLabel(m_widgetTop);
- m_labelTitle->setObjectName("label_title");
- m_labelTitle->setText("Dialog Title");
- m_labelTitle->setFixedSize(120, 18);
- m_labelTitle->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
- /* 初始化关闭按钮 */
- m_pBtn_Close = new QPushButton(m_widgetTop);
- m_pBtn_Close->setObjectName("pBtn_Close");
- m_pBtn_Close->resize(32, 32);
- /* 重新布局顶栏 */
- layoutTop();
- }
- /* 设置top栏的位置布局,标题和关闭按钮的高度起始位置不一样,不方便使用布局,所有手动修改坐标
- * 标题位置32,18,高度18
- * 关闭按钮右对齐16,y12,大小32x32 */
- void DialogBase::layoutTop()
- {
- m_labelTitle->move(32, 18);
- int x = m_widgetTop->width() - m_pBtn_Close->width() - 16;
- m_pBtn_Close->move(x, 12);
- }
- /* 重写鼠标按下事件 */
- void DialogBase::mousePressEvent(QMouseEvent *event)
- {
- m_lastPos = event->globalPos();
- event->accept();
- }
- /* 重写鼠标移动事件 */
- void DialogBase::mouseMoveEvent(QMouseEvent *event)
- {
- auto point = m_widgetTop->mapToGlobal(QPoint(0, 0));
- QRect rect(point, m_widgetTop->size());
- if(!rect.contains(m_lastPos))
- {
- event->accept();
- return;
- }
- int dx = event->globalX() - m_lastPos.x();
- int dy = event->globalY() - m_lastPos.y();
- m_widgetBackground->move(m_widgetBackground->x() + dx, m_widgetBackground->y() + dy);
- m_lastPos = event->globalPos();
- event->accept();
- }
- /* 重写鼠标释放事件 */
- void DialogBase::mouseReleaseEvent(QMouseEvent *event)
- {
- event->accept();
- }
|