Python开发之QT解决无边框界面拖动卡屏问题(附带源码)


Posted in Python onMay 27, 2021

1.简介

看到很多才学QT的人都会问为啥无边框拖动为啥会花屏?

那是因为你每次拖动的过程中都一直在调用move()函数让QT重新绘制界面,如果资源过大,就会导致当前图形还未绘制完,便又重新改变坐标了,从而导致花屏.

2.如何解决

我们参考其它软件,比如QQ,浏览器等,可以看到我们如果在拖动它们的时候,会出现一个虚线框.

如下图所示,可以看到在白色背景下,拖出的虚线框是黑色的

Python开发之QT解决无边框界面拖动卡屏问题(附带源码)

而在黑色背景时,拖出的虚线框是白色的

Python开发之QT解决无边框界面拖动卡屏问题(附带源码)

显然这个虚线框会根据当前桌面的像素点而去取反(也就是255-currentRGB).
解决的过程有两种方法:

1)调用win库来实现

2)自己动手写一个

既然我们已经知道它的实现过程.那我们还是自己动手写一个,只需要写一个虚线框类即可

3.虚线框类代码

DragShadow.h

#ifndef DRAGSHADOW_H
#define DRAGSHADOW_H
#include <QtGui>
class DragShadow : public QWidget
{
  Q_OBJECT
private:
  QImage m_image;
protected:
  bool getInvertColor(int x, int y, QColor &color);
  void paintEvent(QPaintEvent *);
  void showEvent( QShowEvent * event );
public:
  explicit DragShadow(QWidget *parent = 0);
  void setSizePos(int x, int y, int w, int h);
  void setPos(int x,int y );
  void setPos(QPoint pos );
signals:

public slots:

};
#endif // DRAGSHADOW_H

DragShadow.cpp

#include "DragShadow.h"
DragShadow::DragShadow(QWidget *parent) :
QWidget(NULL)
{
  setWindowFlags(Qt::FramelessWindowHint|Qt::Tool);
  setAttribute(Qt::WA_TranslucentBackground);
}
void DragShadow::setSizePos(int x, int y, int w, int h)
{
  if(w%2==0)
    w+=1;
  if(h%2==0)
    h+=1;
  this->setGeometry(x,y,w,h);
}
void DragShadow::setPos(int x,int y )
{
  this->move(x,y);
  this->update();
}
void DragShadow::setPos(QPoint pos )
{
  this->move(pos);
  this->update();
}
void DragShadow::showEvent( QShowEvent * event )
{
   #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))        m_image = QPixmap::grabWindow(QApplication::desktop()->winId()).toImage();   #else        QScreen *screen = QGuiApplication::primaryScreen();        m_image = screen->grabWindow(0).toImage();   #endif
}
void DragShadow::paintEvent(QPaintEvent *)
{
  int LineCount=4;
  QColor color;
  QPainter painter(this);
  painter.setBrush(Qt::NoBrush);
  QPen pen(Qt::SolidLine);
  pen.setColor(Qt::black);
  pen.setWidthF(1);
  painter.setPen(pen);
  painter.drawPoint(0,0);
  for(int current=0;current<LineCount;current++)
  {
    for(int i=current;i<(this->width()-current);i+=2) //x
    {
      this->getInvertColor(this->x()+i,this->y()+current,color);
      pen.setColor(color);
      painter.setPen(pen);
      painter.drawPoint(i,current);            //draw top
      this->getInvertColor(i+this->x(),this->height()-current-1+this->y(),color);
      pen.setColor(color);
      painter.setPen(pen);
      painter.drawPoint(i,this->height()-current-1); //draw bottom
    }
    for(int i=current;i<(this->height()-current);i+=2) //y
    {
      this->getInvertColor(current+this->x(),i+this->y(),color);
      pen.setColor(color);
      painter.setPen(pen);
      painter.drawPoint(current,i);           //draw left
      this->getInvertColor(this->width()-current-1+this->x(),i+this->y(),color);
      pen.setColor(color);
      painter.setPen(pen);
      painter.drawPoint(this->width()-current-1,i); //draw right
    }
  }
}
bool DragShadow::getInvertColor(int x, int y, QColor &color)
{
  int ret=m_image.valid(x,y);
  if(ret)
  {
    QRgb rgb = m_image.pixel(x,y);
    color.setRgb(rgb);
    color.setRed(255-color.red());
    color.setBlue(255-color.blue());
    color.setGreen(255-color.green());
  }
  else
  {
    color.setRed(0);
    color.setBlue(0);
    color.setGreen(0);
  }
  return ret;
}

4.测试UI界面如下图所示

Python开发之QT解决无边框界面拖动卡屏问题(附带源码)

5.拖动时的效果图如下所示

Python开发之QT解决无边框界面拖动卡屏问题(附带源码)

6.针对实线框补充
对于有些不同的windows系统设置,实现的是实线框,如下图所示:

Python开发之QT解决无边框界面拖动卡屏问题(附带源码)

如果想要这种效果,就将上面代码的paintEvent(QPaintEvent *)函数的i+=2改为i++即可.

修改后效果如下所示:

Python开发之QT解决无边框界面拖动卡屏问题(附带源码)

上面的两个不同效果的demo源码地址如下所示:

https://download.csdn.net/download/qq_37997682/13720244

以上就是QT-解决无边框界面拖动卡屏问题(附带源码)的详细内容,更多关于QT无边框界面的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python中利用Scipy包的SIFT方法进行图片识别的实例教程
Jun 03 Python
Django中的CBV和FBV示例介绍
Feb 25 Python
python中多个装饰器的执行顺序详解
Oct 08 Python
python dataframe向下向上填充,fillna和ffill的方法
Nov 28 Python
python 应用之Pycharm 新建模板默认添加编码格式-作者-时间等信息【推荐】
Jun 17 Python
Django rest framework jwt的使用方法详解
Aug 08 Python
Python二元赋值实用技巧解析
Oct 25 Python
win7上tensorflow2.2.0安装成功 引用DLL load failed时找不到指定模块 tensorflow has no attribute xxx 解决方法
May 20 Python
pytorch快速搭建神经网络_Sequential操作
Jun 17 Python
如何在scrapy中集成selenium爬取网页的方法
Nov 18 Python
详解pandas中利用DataFrame对象的.loc[]、.iloc[]方法抽取数据
Dec 13 Python
基于Python实现将列表数据生成折线图
Mar 23 Python
pytorch 实现在测试的时候启用dropout
使用Python脚本对GiteePages进行一键部署的使用说明
教你使用Python pypinyin库实现汉字转拼音
基于tensorflow权重文件的解读
May 26 #Python
解决Python字典查找报Keyerror的问题
浅谈tf.train.Saver()与tf.train.import_meta_graph的要点
tensorflow中的数据类型dtype用法说明
May 26 #Python
You might like
建站常用13种PHP开源CMS比较
2009/08/23 PHP
php设计模式 Mediator (中介者模式)
2011/06/26 PHP
php在线代理转向代码
2012/05/05 PHP
PHP与MYSQL中UTF8编码的中文排序实例
2014/10/21 PHP
Linux下安装PHP MSSQL扩展教程
2014/10/24 PHP
浅析PHP文件下载原理
2014/12/25 PHP
详解php中反射的应用
2016/03/15 PHP
Laravel学习教程之View模块详解
2017/09/18 PHP
PHP流Streams、包装器wrapper概念与用法实例详解
2017/11/17 PHP
php框架CodeIgniter主从数据库配置方法分析
2018/05/25 PHP
基于jquery的跨域调用文件
2010/11/19 Javascript
jquery序列化表单去除指定元素示例代码
2014/04/10 Javascript
jQuery模拟点击A标记示例参考
2014/04/17 Javascript
JavaScript学习笔记(三):JavaScript也有入口Main函数
2015/09/12 Javascript
详解Bootstrap四种图片样式
2016/01/04 Javascript
又一枚精彩的弹幕效果jQuery实现
2016/07/25 Javascript
CSS3 media queries结合jQuery实现响应式导航
2016/09/30 Javascript
jquery动态添加以及遍历option并获取特定样式名称的option方法
2018/01/29 jQuery
Vue 去除路径中的#号
2018/04/19 Javascript
js如何找出字符串中的最长回文串
2018/06/04 Javascript
vue中axios的封装问题(简易版拦截,get,post)
2018/06/15 Javascript
小程序实现展开/收起的效果示例
2018/09/22 Javascript
jQuery操作cookie的示例代码
2019/06/05 jQuery
Vue.js项目实战之多语种网站的功能实现(租车)
2019/08/07 Javascript
[38:42]完美世界DOTA2联赛循环赛 Matador vs Forest BO2第二场 11.05
2020/11/05 DOTA
Python实现模拟分割大文件及多线程处理的方法
2017/10/10 Python
Python(Django)项目与Apache的管理交互的方法
2018/05/16 Python
python中scikit-learn机器代码实例
2018/08/05 Python
python中文编码与json中文输出问题详解
2018/08/24 Python
python ffmpeg任意提取视频帧的方法
2020/02/21 Python
使用python修改文件并立即写回到原始位置操作(inplace读写)
2020/06/28 Python
OSPREY LONDON官网:英国本土皮具品牌
2019/05/31 全球购物
泰国在线书店:SE-ED
2020/06/21 全球购物
2015年女生节活动总结
2015/02/27 职场文书
深入浅析React中diff算法
2021/05/19 Javascript
SQL Server使用PIVOT与unPIVOT实现行列转换
2022/05/25 SQL Server