Qt 不规则窗口(窗口边框隐藏,并能拖动)

Qt 不规则窗口

实现不规则窗口,并能拖动

/*VQShapeWidget.h*/

#ifndef VQDRAGABLEWIDGET_H
#define VQDRAGABLEWIDGET_H

#include "QWidget"

class VQShapeWidget : public QWidget
{
  Q_OBJECT; //QT信号机制必须

public :
  VQShapeWidget( QWidget *parent, char *path);
  void setBackgroundImage( char *path);

protected :
  void mousePressEvent(QMouseEvent * event);
  void mouseReleaseEvent(QMouseEvent * event);
  void mouseMoveEvent(QMouseEvent * event);
  void paintEvent(QPaintEvent * event);

private :
  QPoint _dragStartPos;
  bool _isLeftClicking;
  QPixmap _backgroundImage;
  void setSize( int x, int y);
};

#endif // VQDRAGABLEWIDGET_H

/*VQShapeWidget.cpp*/

#include "stdafx.h"

#include "VQDragableWidget.h"

VQShapeWidget::VQShapeWidget( QWidget *parent, char *path)
  :QWidget(parent, Qt::WindowType::FramelessWindowHint)
{
  _isLeftClicking = false;
  setBackgroundImage(path);
  setSize(800, 600);
}

void VQShapeWidget::mousePressEvent( QMouseEvent * event )
{
  if( event->button() == Qt::LeftButton)
  {
    _isLeftClicking = true;
    _dragStartPos = event ->globalPos()-frameGeometry().topLeft();
    event->accept();
  }
}

void VQShapeWidget::mouseMoveEvent( QMouseEvent * event )
{
  if(_isLeftClicking)
  {
    move( event->globalPos()-_dragStartPos);
    event->accept();
  }
}

void VQShapeWidget::mouseReleaseEvent( QMouseEvent * event )
{
  if( event->button() == Qt::LeftButton)
  {
    _isLeftClicking = false;
  }
}

void VQShapeWidget::paintEvent( QPaintEvent * event )
{
  QPainter painter( this);
  painter.drawPixmap(0, 0, _backgroundImage);
}

void VQShapeWidget::setBackgroundImage( char *path)
{
  _backgroundImage.load(path, 0, Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);
  resize(_backgroundImage.size()); //设置窗口的尺寸为图片的尺寸
  setMask(_backgroundImage.mask()); //设置窗口过滤
}

void VQShapeWidget::setSize( int x, int y )
{
  _backgroundImage = _backgroundImage.scaled(x, y);
  resize(_backgroundImage.size());
  setMask(_backgroundImage.mask());
}



  

你可能感兴趣的:(Qt,c++)