Python開發(fā)之QT解決無邊框界面拖動(dòng)卡屏問題(附帶源碼)
1.簡介
看到很多才學(xué)QT的人都會(huì)問為啥無邊框拖動(dòng)為啥會(huì)花屏?
那是因?yàn)槟忝看瓮蟿?dòng)的過程中都一直在調(diào)用move()函數(shù)讓QT重新繪制界面,如果資源過大,就會(huì)導(dǎo)致當(dāng)前圖形還未繪制完,便又重新改變坐標(biāo)了,從而導(dǎo)致花屏.
2.如何解決
我們參考其它軟件,比如QQ,瀏覽器等,可以看到我們?nèi)绻谕蟿?dòng)它們的時(shí)候,會(huì)出現(xiàn)一個(gè)虛線框.
如下圖所示,可以看到在白色背景下,拖出的虛線框是黑色的

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

顯然這個(gè)虛線框會(huì)根據(jù)當(dāng)前桌面的像素點(diǎn)而去取反(也就是255-currentRGB).
解決的過程有兩種方法:
1)調(diào)用win庫來實(shí)現(xiàn)
2)自己動(dòng)手寫一個(gè)
既然我們已經(jīng)知道它的實(shí)現(xiàn)過程.那我們還是自己動(dòng)手寫一個(gè),只需要寫一個(gè)虛線框類即可
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界面如下圖所示

5.拖動(dòng)時(shí)的效果圖如下所示

6.針對(duì)實(shí)線框補(bǔ)充
對(duì)于有些不同的windows系統(tǒng)設(shè)置,實(shí)現(xiàn)的是實(shí)線框,如下圖所示:

如果想要這種效果,就將上面代碼的paintEvent(QPaintEvent *)函數(shù)的i+=2改為i++即可.
修改后效果如下所示:

上面的兩個(gè)不同效果的demo源碼地址如下所示:
http://xiazai.jb51.net/202105/yuanma/DragTest_jb51.rar
以上就是QT-解決無邊框界面拖動(dòng)卡屏問題(附帶源碼)的詳細(xì)內(nèi)容,更多關(guān)于QT無邊框界面的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python報(bào)錯(cuò)ImportError: No module named ‘mi
在 Python 開發(fā)過程中,報(bào)錯(cuò)是常有的事,而當(dāng)遇到“ImportError: No module named ‘missing_module’”這樣的報(bào)錯(cuò)時(shí),可能會(huì)讓開發(fā)者感到困惑和苦惱,本文將深入探討這個(gè)報(bào)錯(cuò)的原因和解決方法,幫助開發(fā)者快速解決這個(gè)問題,需要的朋友可以參考下2024-10-10
keras 兩種訓(xùn)練模型方式詳解fit和fit_generator(節(jié)省內(nèi)存)
這篇文章主要介紹了keras 兩種訓(xùn)練模型方式詳解fit和fit_generator(節(jié)省內(nèi)存),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
python讀取json數(shù)據(jù)還原表格批量轉(zhuǎn)換成html
這篇文章主要介紹了python讀取json數(shù)據(jù)還原表格批量轉(zhuǎn)換成html,由于需要對(duì)ocr識(shí)別系統(tǒng)的表格識(shí)別結(jié)果做驗(yàn)證,通過返回的json文件結(jié)果對(duì)比比較麻煩,故需要將json文件里面的識(shí)別結(jié)果還原為表格做驗(yàn)證,下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-03-03
解決import tensorflow as tf 出錯(cuò)的原因
這篇文章主要介紹了解決import tensorflow as tf 出錯(cuò)的原因,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
python備份文件以及mysql數(shù)據(jù)庫的腳本代碼
最近正在學(xué)習(xí)python,看了幾天了,,所以寫個(gè)小腳本練習(xí)練習(xí),沒什么含金量,只當(dāng)練手2013-06-06
Python中構(gòu)建終端應(yīng)用界面利器Blessed模塊的使用
Blessed?庫作為一個(gè)輕量級(jí)且功能強(qiáng)大的解決方案,開始在開發(fā)者中贏得口碑,今天,我們就一起來探索一下它是如何讓終端UI開發(fā)變得輕松而高效的吧2025-01-01

