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 re正则表达式模块(Regular Expression)
Jul 16 Python
Python中的time模块与datetime模块用法总结
Jun 30 Python
django站点管理详解
Dec 12 Python
详解如何利用Cython为Python代码加速
Jan 27 Python
解决python nohup linux 后台运行输出的问题
May 11 Python
python 文件转成16进制数组的实例
Jul 09 Python
Django中的Model操作表的实现
Jul 24 Python
Python初学者需要注意的事项小结(python2与python3)
Sep 26 Python
Python matplotlib通过plt.scatter画空心圆标记出特定的点方法
Dec 13 Python
关于python pycharm中输出的内容不全的解决办法
Jan 10 Python
Python文件名匹配与文件复制的实现
Dec 11 Python
opencv用VS2013调试时用Image Watch插件查看图片
Jul 26 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
如何解决CI框架的Disallowed Key Characters错误提示
2013/07/05 PHP
PHP中的socket_read和socket_recv区别详解
2015/02/09 PHP
分享微信扫码支付开发遇到问题及解决方案-附Ecshop微信支付插件
2015/08/23 PHP
php常用图片处理类
2016/03/16 PHP
Laravel实现自定义错误输出内容的方法
2016/10/10 PHP
转自Jquery官方 jQuery1.1.3发布,速度提升800%,体积保持20K
2007/08/19 Javascript
解决jquery submit()提交表单提示:f[s] is not a function
2013/01/23 Javascript
js日期时间补零的小例子
2013/03/05 Javascript
JavaScript 函数参数是传值(byVal)还是传址(byRef) 分享
2013/07/02 Javascript
JavaScript动态操作表格实例(添加,删除行,列及单元格)
2013/11/25 Javascript
jquery判断小数点两位和自动删除小数两位后的数字
2014/03/19 Javascript
javascript中setTimeout的问题解决方法
2014/05/08 Javascript
适用于javascript开发者的Processing.js入门教程
2016/02/24 Javascript
理解javascript对象继承
2016/04/17 Javascript
jquery.validate使用详解
2016/06/02 Javascript
jQuery mobile在页面加载时添加加载中效果 document.ready 和window.onload执行顺序比较
2016/07/14 Javascript
jQuery与JavaScript节点创建方法的对比
2016/11/18 Javascript
基于jQuery制作小图标上下滑动特效
2017/01/18 Javascript
从零开始做一个pagination分页组件
2017/03/15 Javascript
mac上配置Android环境变量的方法
2018/07/08 Javascript
学习React中ref的两个demo示例
2018/08/14 Javascript
jquery插件开发模式实例详解
2019/07/20 jQuery
js设计模式之代理模式及订阅发布模式实例详解
2019/08/15 Javascript
Javascript异步执行不按顺序解决方案
2020/04/30 Javascript
[04:02]DOTA2上海特锦赛小组赛第二日recap精彩回顾
2016/02/28 DOTA
Python去除列表中重复元素的方法
2015/03/20 Python
详解Python中的Cookie模块使用
2015/07/06 Python
50行Python代码实现人脸检测功能
2018/01/23 Python
pycharm创建一个python包方法图解
2019/04/10 Python
利用anaconda保证64位和32位的python共存
2021/03/09 Python
python利用7z批量解压rar的实现
2019/08/07 Python
python3安装OCR识别库tesserocr过程图解
2020/04/02 Python
html5 datalist 选中option选项后的触发事件
2020/03/05 HTML / CSS
会议通知
2015/04/15 职场文书
2015年暑期见闻
2015/07/14 职场文书
CSS使用伪类控制边框长度的方法
2022/01/18 HTML / CSS