婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Python開發之QT解決無邊框界面拖動卡屏問題(附帶源碼)

Python開發之QT解決無邊框界面拖動卡屏問題(附帶源碼)

熱門標簽:天津電話機器人公司 手機網頁嵌入地圖標注位置 應電話機器人打電話違法嗎 開封自動外呼系統怎么收費 開封語音外呼系統代理商 電銷機器人的風險 400電話辦理哪種 河北防封卡電銷卡 地圖標注線上如何操作

1.簡介

看到很多才學QT的人都會問為啥無邊框拖動為啥會花屏?

那是因為你每次拖動的過程中都一直在調用move()函數讓QT重新繪制界面,如果資源過大,就會導致當前圖形還未繪制完,便又重新改變坐標了,從而導致花屏.

2.如何解決

我們參考其它軟件,比如QQ,瀏覽器等,可以看到我們如果在拖動它們的時候,會出現一個虛線框.

如下圖所示,可以看到在白色背景下,拖出的虛線框是黑色的

而在黑色背景時,拖出的虛線框是白色的

顯然這個虛線框會根據當前桌面的像素點而去取反(也就是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;currentLineCount;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界面如下圖所示

5.拖動時的效果圖如下所示

6.針對實線框補充
對于有些不同的windows系統設置,實現的是實線框,如下圖所示:

如果想要這種效果,就將上面代碼的paintEvent(QPaintEvent *)函數的i+=2改為i++即可.

修改后效果如下所示:

上面的兩個不同效果的demo源碼地址如下所示:

http://xiazai.jb51.net/202105/yuanma/DragTest_jb51.rar

以上就是QT-解決無邊框界面拖動卡屏問題(附帶源碼)的詳細內容,更多關于QT無邊框界面的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • 解決PyQt5 無邊框后窗口的移動問題
  • PyQt5 實現給無邊框widget窗口添加背景圖片
  • Qt無邊框窗口拖拽和陰影的實現方法
  • Qt透明無邊框窗口的實現示例
  • Pyqt實現無邊框窗口拖動以及窗口大小改變
  • PyQt5實現無邊框窗口的標題拖動和窗口縮放

標簽:六盤水 常州 山東 駐馬店 成都 江蘇 蘭州 宿遷

巨人網絡通訊聲明:本文標題《Python開發之QT解決無邊框界面拖動卡屏問題(附帶源碼)》,本文關鍵詞  Python,開,發之,解,決無,邊框,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python開發之QT解決無邊框界面拖動卡屏問題(附帶源碼)》相關的同類信息!
  • 本頁收集關于Python開發之QT解決無邊框界面拖動卡屏問題(附帶源碼)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 阳东县| 凤阳县| 雅江县| 浪卡子县| 木兰县| 镇安县| 河北省| 蓬莱市| 凤翔县| 尼玛县| 莱阳市| 改则县| 叶城县| 昌宁县| 台东县| 闵行区| 高邮市| 永登县| 得荣县| 大新县| 洪泽县| 五河县| 江阴市| 山阴县| 巴楚县| 新津县| 鹤岗市| 定边县| 鹿邑县| 闻喜县| 永春县| 尚义县| 新沂市| 广南县| 抚远县| 泗水县| 临桂县| 正安县| 玛纳斯县| 镇远县| 洛阳市|