优点
相对于隐藏标题栏只重写三个鼠标函数的标题栏,本标题栏有边框,可以伸缩,双击放大,鼠标拖动到屏幕顶端放大,拖动响应快等
资源文件的图标自己找
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMouseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
bool eventFilter(QObject *obj, QEvent *e);
//void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private slots:
void on_btnMax_released();
void on_btnMin_released();
void on_btnExit_released();
private:
Ui::MainWindow *ui;
//标题拖动、双击最大化
bool mDrag;
QPoint mDragPos;
bool mIsMax;
QRect mLocation;
QFont mIconFont;
//与缩放相关的变量
int mZoom;
QRect mZoomLocation;
QPoint mZoomPos;
};
#endif // MAINWINDOW_H
cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowFlag(Qt::FramelessWindowHint);
//标题栏
mLocation = this->geometry();//当前大小
mIsMax = false;//最大窗口标记
mDrag = false;//按下标记
ui->widgetTitle->installEventFilter(this);//标题栏安装过滤器
//状态栏
mZoom = false;
//给缩放的图片label处理鼠标的按下、移动、弹起消息,进行缩放窗口功能
ui->labelZoom->installEventFilter(this);
ui->labelZoom->setCursor(Qt::SizeFDiagCursor);//设置光标的形状
}
MainWindow::~MainWindow()
{
delete ui;
}
//事件过滤器
bool MainWindow::eventFilter(QObject *obj, QEvent *e)
{
if (obj == ui->widgetTitle)
{
if(e->type() == QEvent::MouseButtonDblClick)
{
on_btnMax_released();
return true;
}
else if(e->type()==QEvent::MouseButtonPress)
{
QMouseEvent *event = (QMouseEvent *)e;
if ((event->button() == Qt::LeftButton) & !mIsMax)
{
mDrag = true;
mDragPos = event->globalPos() - pos();
event->accept();
return true;
}
else if ((event->button() == Qt::LeftButton) & mIsMax)
{
mDrag=true;
return true;
}
}
}
else if (obj == ui->labelZoom)
{
//实现拖动右下角缩放窗口
if(e->type() == QEvent::MouseButtonPress)
{
QMouseEvent *event = (QMouseEvent *)e;
if (event->buttons() & Qt::LeftButton)
{
if(mIsMax)
{
//已最大化,就不让再拖动
return true;
}
mZoom = true;
mZoomLocation = geometry();
mZoomPos = event->globalPos();
return true;
}
}
else if(e->type() == QEvent::MouseMove)
{
QMouseEvent *event = (QMouseEvent *)e;
if (mZoom && (event->buttons() & Qt::LeftButton))
{
int dx = event->globalPos().x() - mZoomPos.x();
int dy = event->globalPos().y() - mZoomPos.y();
QRect rc = mZoomLocation;
rc.setRight(rc.right() + dx);
rc.setBottom(rc.bottom() + dy);
setGeometry(rc);
update();
return true;
}
}
else if(e->type() == QEvent::MouseButtonRelease)
{
mZoom = false;
return true;
}
}
return QObject::eventFilter(obj, e);
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if (mDrag && (event->buttons() & Qt::LeftButton))
{
if(mIsMax)
{
on_btnMax_released();
}
move(event->globalPos() - mDragPos);
event->accept();
}
return;
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if(event->globalPos().y()==0)
{
on_btnMax_released();
}
mDrag = false;
}
void MainWindow::on_btnMax_released()
{
if (mIsMax)
{
ui->btnMax->setIcon(QIcon(":/new/prefix1/max1.png"));
setGeometry(mLocation);
}
else
{
ui->btnMax->setIcon(QIcon(":/new/prefix1/max.png"));
mLocation = geometry();
setGeometry(qApp->desktop()->availableGeometry());//不包含任务栏的桌面大小
}
mIsMax = !mIsMax;
}
void MainWindow::on_btnMin_released()
{
showMinimized();
}
void MainWindow::on_btnExit_released()
{
qApp->exit();
}