#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);
mLocation = this->geometry();
mIsMax = false;
mDrag = false;
mZoom = false;
ui->widgetTitle->installEventFilter(this);
ui->btnMin->setIcon(QIcon(":/image/min.png"));
ui->btnMax->setIcon(QIcon(":/image/max1.png"));
ui->btnExit->setIcon(QIcon(":/image/exit.png"));
//给缩放的图片label处理鼠标的按下、移动、弹起消息,进行缩放窗口功能
ui->labelZoom->installEventFilter(this);
ui->labelZoom->setCursor(Qt::SizeFDiagCursor);
setStyleSheet("QMainWindow{color:#E8E8E8;background:#43CD80;}");
//另外在设计器内查看widgetTitle样式,内有标题栏各个子控件的样式设置
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *obj, QEvent *e)
{
if (obj == ui->widgetTitle)
{
if(e->type() == QEvent::MouseButtonDblClick)
{
on_btnMax_clicked();
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::mousePressEvent(QMouseEvent *e)//鼠标按下事件
{
if (e->button() == Qt::LeftButton)
{
mDrag = true;
mDragPos = e->globalPos() - pos();
e->accept();
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)//鼠标移动事件
{
if (mDrag && (e->buttons() && Qt::LeftButton))
{
move(e->globalPos() - mDragPos);
e->accept();
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *e)//鼠标释放事件
{
mDrag = false;
}
void MainWindow::on_btnMin_clicked()
{
showMinimized();
}
void MainWindow::on_btnMax_clicked()
{
if (mIsMax)
{
setGeometry(mLocation);
ui->btnMax->setIcon(QIcon(":/image/max1.png"));
ui->btnMax->setToolTip(QStringLiteral("最大化"));
}
else
{
mLocation = geometry();
setGeometry(qApp->desktop()->availableGeometry());
ui->btnMax->setIcon(QIcon(":/image/max2.png"));
ui->btnMax->setToolTip(QStringLiteral("还原"));
}
mIsMax = !mIsMax;
}
void MainWindow::on_btnExit_clicked()
{
qApp->exit();
}
Qt 实现无边框窗口,支持缩放窗口大小
5星 · 超过95%的资源 需积分: 50 23 浏览量
2017-09-26
02:25:36
上传
评论 2
收藏 20KB RAR 举报
逆枫゛
- 粉丝: 2336
- 资源: 19
最新资源
- 基于pytorch使用LSTM、GRU、BPNN进行时间序列预测源码+模型.zip
- 课程设计基于Tensorflow的手势识别+数据集+运行说明业.zip
- C++毕业设计基于QT的运动场地预约登记管理系统源码+数据库.zip
- 城市降雨对地铁短期OD客流的影响路径与机制研究项目python源码+数据集+PPT演讲.zip
- vcu-gst-app源码包
- 基于QT和C++实现的运动场地预约登记系统源码+数据库+系统截图展示.zip
- 时间序列预测的基准模型RELM-RKELM-RESN-RSVR-matlab源码+数据集.zip
- 电子通信设计资料三闪灯DXP资料
- LMD局域均值分解信号分量可视化(Matlab完整源码和数据)
- 电子通信设计资料频率PWM控制均可调模块DXP资料及其相关资料
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈