qt 窗口无标题在桌面移动,不可移出可视范围之外
来源:互联网 发布:bt python源代码下载 编辑:程序博客网 时间:2024/04/29 22:21
下面是基类的源代码,把所需求移动的窗口类继承这个基类即可
头文件:
/************************************************************************//*BaseWidget.h *//************************************************************************/#ifndef BASEWIDGET_H#define BASEWIDGET_H#include <QWidget>class BaseWidget : public QWidget{Q_OBJECTpublic:BaseWidget(QWidget *parent = 0);~BaseWidget();protected:void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent*event);bool m_moving;//用来标记是否鼠标移动QPoint m_offset;private:};#endif // BASEWIDGET_H
CPP文件:
/************************************************************************//* BaseWidget.cpp *//************************************************************************/#include "BaseWidget.h"#include <QMouseEvent>#include <QDesktopWidget>#include <QApplication>BaseWidget::BaseWidget(QWidget *parent): QWidget(parent,Qt::FramelessWindowHint),m_moving(false){}BaseWidget::~BaseWidget(){}void BaseWidget::mousePressEvent( QMouseEvent *event ){if((event->button() == Qt::LeftButton)){m_moving = true;m_offset = event->pos();}}void BaseWidget::mouseMoveEvent( QMouseEvent *event ){if(m_moving){//方法1:QDesktopWidget* desktop = QApplication::desktop();QRect windowRect(desktop->availableGeometry());QRect widgetRect(this->geometry());QPoint point(event->globalPos() - m_offset);//以下是防止窗口拖出可见范围外//左边if (point.x() <= 0){point = QPoint(0,point.y());}//右边int y = windowRect.bottomRight().y() - this->size().height();if (point.y() >= y && widgetRect.topLeft().y() >= y){point = QPoint(point.x(),y);}//上边if (point.y() <= 0){point = QPoint(point.x(),0);}//下边int x = windowRect.bottomRight().x() - this->size().width();if (point.x() >= x && widgetRect.topLeft().x() >= x){point = QPoint(x,point.y());}move(point);//方法2://可以通过判断QRect windowRect是否包含(contains) QRect widgetRect 再移动//这里没有给出代码}//如果只是要求移动窗口,用以下代码即可实现//move(event->globalPos() - m_offset);}void BaseWidget::mouseReleaseEvent( QMouseEvent*event ){if(event->button() == Qt::LeftButton)m_moving = false;}
0 0
- qt 窗口无标题在桌面移动,不可移出可视范围之外
- qt实现无标题窗口移动范围
- Qt 实现无标题窗口的移动
- 实现Qt无标题窗口移动的改进
- QT无标题窗口在任务栏显示关闭
- Qt 在可视窗口部件上绘图
- Qt 在可视窗口部件上绘图
- 无标题窗口的移动
- 无标题窗口的移动
- 无标题窗口如何移动
- C#移动无标题窗口
- 移动无标题窗口(delphi)
- C#移动无标题窗口
- NSIS无标题窗口移动
- Win7下窗口移出桌面范围 无法还原情况的解决
- 无标题窗口移动及缩放
- QT 鼠标拖动无标题窗口
- qt设置窗口在桌面居中显示
- 欢迎使用CSDN-markdown编辑器
- burpsuite调试web微信时提示 unrecognized_name的解决办法
- ListView的常见优化:获取网络图片异步加载,分批加载,分页显示,图片缓存等优化方式
- 仿暴风影音安卓客户端应用源码
- 【Quartz】Quartz存储与持久化-基于Spring的配置
- qt 窗口无标题在桌面移动,不可移出可视范围之外
- android-命令模式在项目中的应用
- Word2007如何从任意页开始设置页码 word07页码设置毕业论文
- HDU2062——Subset sequence
- Linux hwclock命令参数及用法详解
- 行内元素和块级元素有哪些
- 仿暴风影音安卓客户端应用源码
- js时钟
- 验证字符串是否是手机号