//指定窗口为无边框
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
重写鼠标事件:
void mousePressEvent(QMouseEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;
定义位置:
QPoint diff_pos;//鼠标和窗口的相对位移QPoint window_pos;//绝对位置QPoint mouse_pos;
void QtWidgetsApplication2::mousePressEvent(QMouseEvent * event)
{mouse_pos = event->globalPos();window_pos = this->pos();diff_pos = mouse_pos - window_pos;}void QtWidgetsApplication2::mouseMoveEvent(QMouseEvent * event)
{QPoint pos = event->globalPos();this->move(pos - diff_pos);
}
再创建一个widget作为标题栏,并定义好控件:
#include <QWidget>
#include <QLabel>
#include <QPushButton>class CTitleBar : public QWidget
{Q_OBJECTpublic:CTitleBar(QWidget *parent = nullptr);~CTitleBar();private:void init();private:QLabel* logo;QLabel* title;QPushButton* set;QPushButton* min;QPushButton* max;QPushButton* close;
};
初始化控件:
void CTitleBar::initUI()
{logo = new QLabel(this);title = new QLabel(this);title->setText(u8"标题");title->setFixedWidth(120);set = new QPushButton(this);set->setFixedSize(32, 32);min = new QPushButton(this);min->setFixedSize(32, 32);max = new QPushButton(this);max->setFixedSize(32, 32);close = new QPushButton(this);close->setFixedSize(32, 32);QHBoxLayout* lay = new QHBoxLayout(this);lay->addWidget(logo);lay->addWidget(title);lay->addWidget(set);lay->addWidget(min);lay->addWidget(max);lay->addWidget(close);lay->setContentsMargins(5, 5, 5,5);
}
实现无边窗口的拉伸、改变大小:
重写nativeEvent:
#include <qt_windows.h>
bool nativeEvent(const QByteArray& eventType, void* message, long* result) override;bool QtWidgetsApplication2::nativeEvent(const QByteArray & eventType, void * message, long * result)
{MSG* param = static_cast<MSG*>(message);switch (param->message){case WM_NCHITTEST:{int nX = GET_X_LPARAM(param->lParam) - this->geometry().x();int nY = GET_Y_LPARAM(param->lParam) - this->geometry().y();/*if (childAt(nX, nY) != nullptr)return QWidget::nativeEvent(eventType, message, result);*/if (nX > m_nBorderWidth && nX < this->width() - m_nBorderWidth &&nY > m_nBorderWidth && nY < this->height() - m_nBorderWidth){if (childAt(nX, nY) != nullptr)return QWidget::nativeEvent(eventType, message, result);}if ((nX > 0) && (nX < m_nBorderWidth))*result = HTLEFT;if ((nX > this->width() - m_nBorderWidth) && (nX < this->width()))*result = HTRIGHT;if ((nY > 0) && (nY < m_nBorderWidth))*result = HTTOP;if ((nY > this->height() - m_nBorderWidth) && (nY < this->height()))*result = HTBOTTOM;if ((nX > 0) && (nX < m_nBorderWidth) && (nY > 0)&& (nY < m_nBorderWidth))*result = HTTOPLEFT;if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())&& (nY > 0) && (nY < m_nBorderWidth))*result = HTTOPRIGHT;if ((nX > 0) && (nX < m_nBorderWidth)&& (nY > this->height() - m_nBorderWidth) && (nY < this->height()))*result = HTBOTTOMLEFT;if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())&& (nY > this->height() - m_nBorderWidth) && (nY < this->height()))*result = HTBOTTOMRIGHT;return true;}}return false;
}
启用鼠标悬停事件:
setAttribute(Qt::WA_Hover)