#include "xmainwindow.h" XMainWindow::XMainWindow(QWidget *parent) : QWidget(parent) { createFrame(); createWidget(); createLayout(); initVariables(); initUI(); } XMainWindow::~XMainWindow() { delete m_pTitleBar; delete m_pToolBar; delete m_pStatusBar; delete m_pLayout; } void XMainWindow::createFrame() { this->resize(500,300); this->setWindowFlags(Qt::FramelessWindowHint); this->setStyleSheet("background:red"); this->setMouseTracking(true); } void XMainWindow::createWidget() { } void XMainWindow::createLayout() { } void XMainWindow::initUI() { } void XMainWindow::initVariables() { m_bMaxWin = false; m_mouseResizeWindowFlag = false; m_mouseMoveWindowFlag = false; m_rectRestoreWindow = geometry(); m_pContentWidget = new QWidget(this); m_pTitleBar = new XTitleBar(this); m_pToolBar = new XToolBar(this); m_pStatusBar = new XStatusBar(this); connect(m_pTitleBar,SIGNAL(signalMoveOffset(QPoint)),this,SLOT(slotOnMoveOffset(QPoint))); connect(m_pTitleBar,SIGNAL(signalClose()),this,SLOT(slotOnClose())); connect(m_pTitleBar,SIGNAL(signalMin()),this,SLOT(slotOnMin())); connect(m_pTitleBar,SIGNAL(signalMaxRestore()),this,SLOT(slotOnMaxRestore())); m_pContentWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); m_pContentWidget->setStyleSheet("background:white"); m_pContentWidget->setMouseTracking(true); m_pLayout = new QVBoxLayout(this); m_pLayout->addWidget(m_pTitleBar); m_pLayout->addWidget(m_pToolBar); m_pLayout->addWidget(m_pContentWidget); m_pLayout->addWidget(m_pStatusBar); m_pLayout->setContentsMargins(1,1,1,1); m_pLayout->setSpacing(0); } void XMainWindow::slotOnMoveOffset(QPoint offset) { m_mouseResizeWindowFlag = false; if(m_bMaxWin) slotOnMaxRestore(); move(this->pos()+offset); } void XMainWindow::slotOnMin() { setWindowState(Qt::WindowMinimized); } void XMainWindow::slotOnMaxRestore() { if(!m_bMaxWin) { m_rectRestoreWindow = geometry(); setGeometry(qApp->desktop()->availableGeometry()); m_bMaxWin = true; } else { setGeometry(m_rectRestoreWindow); m_bMaxWin = false; } } void XMainWindow::slotOnClose() { close(); } void XMainWindow::paintEvent(QPaintEvent *event) { QBitmap bitmap(this->size()); QPainter painter(&bitmap); painter.fillRect(bitmap.rect(), Qt::white); painter.setBrush(QColor(0, 0, 0)); painter.drawRoundedRect(QRect(0, 0, this->width(), this->height()), 2, 2); setMask(bitmap); QPainter painter1(this); painter1.drawPixmap(rect(), QPixmap(":/skin/default")); } void XMainWindow::mouseDoubleClickEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton && event->y()<= m_pTitleBar->height()) { slotOnMaxRestore(); } } void XMainWindow::mouseMoveEvent(QMouseEvent *event) { if(!m_mouseResizeWindowFlag) { m_eDirection = getPointPos(event->x(),event->y()); qDebug()<pos()<globalPos(),m_eDirection); m_mouseSrcPos = event->globalPos(); } } // qDebug()<button()<button() == Qt::LeftButton) && m_mouseMoveWindowFlag) // { // static QWidget* parent_widget = this->parentWidget(); // QPoint parent_point = this->pos(); // parent_point.setX(parent_point.x() + event->x() - m_mouseSrcPos.x()); // parent_point.setY(parent_point.y() + event->y() - m_mouseSrcPos.y()); // this->move(parent_point); // } } void XMainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_mouseSrcPos = event->globalPos(); m_mouseResizeWindowFlag = true; // if(event->y()<= m_pTitleBar->height() + m_pToolBar->height() && event->y()>1) // { // m_mouseMoveWindowFlag = true; // } } } void XMainWindow::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_mouseResizeWindowFlag = false; m_mouseMoveWindowFlag = false; } } void XMainWindow::moveEvent(QMoveEvent *event) { //如果窗口最大化,移动时将窗口还原到正常状态 if(m_bMaxWin) { slotOnMaxRestore(); } } void XMainWindow::setCursorStyle(enum_Direction direction) { if(m_bMaxWin) return ; //设置上下左右以及右上、右下、左上、坐下的鼠标形状 switch(direction) { case eTop: case eBottom: setCursor(Qt::SizeVerCursor); break; case eRight: case eLeft: setCursor(Qt::SizeHorCursor); break; case eTopRight: case eBottomLeft: setCursor(Qt::SizeBDiagCursor); break; case eRightBottom: case eLeftTop: setCursor(Qt::SizeFDiagCursor); break; default: setCursor(Qt::ArrowCursor); break; } } //确定点的位置方向 XMainWindow::enum_Direction XMainWindow::getPointPos(int nXRelative,int nYRelative) { qDebug()<