Python 이 개발 한 QT 는 테두리 없 는 인터페이스 드래그 카드 화면 문 제 를 해결 합 니 다(원본 코드 추가)

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;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.드래그 할 때의 효과 도 는 다음 과 같다.

6.실선 상자 에 대한 보충
몇몇 다른 윈도 우즈 시스템 설정 에 대해 다음 그림 과 같이 실선 상 자 를 실현 합 니 다.

이 효 과 를 원 하 시 면 위 코드 의 paintEvent(QPaintEvent*)함수 의 i+2 를 i++로 변경 하 시 면 됩 니 다.
수정 후 효 과 는 다음 과 같다.

위의 두 가지 서로 다른 효과 의 demo 소스 주 소 는 다음 과 같다.
http://xiazai.jb51.net/202105/yuanma/DragTest_jb51.rar
이상 은 QT 입 니 다.테두리 없 는 인터페이스 드래그 카드 문제(원본 코드 추가)를 해결 하 는 상세 한 내용 입 니 다.QT 테두리 없 는 인터페이스 에 관 한 자 료 는 다른 관련 글 을 주목 하 십시오!

좋은 웹페이지 즐겨찾기