实现:
可以借助不规则的位图来实现不规则窗体,并支持拖动和移动。下面分别是头文件和源文件
cpp
#ifndef SHAPEWIDGET_H
#define SHAPEWIDGET_H
#include <QWidget>
class ShapeWidget : public QWidget
{
Q_OBJECT
public:
ShapeWidget(QWidget *parent = 0);
~ShapeWidget();
protected:
void mousePressEvent(QMouseEvent *);
void mouseMoveEvent(QMouseEvent *);
void paintEvent(QPaintEvent *);
private:
QPoint dragPosition;
};
#endif // SHAPEWIDGET_H
cpp
#include "shapewidget.h"
#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QBitmap>
ShapeWidget::ShapeWidget(QWidget *parent)
: QWidget(parent)
{
QPixmap pix;
pix.load("16.png",0,Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);
resize(pix.size());
setMask(QBitmap(pix.mask()));
setWindowFlag(Qt::FramelessWindowHint);
}
ShapeWidget::~ShapeWidget()
{
}
void ShapeWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
dragPosition =event->globalPos()-frameGeometry().topLeft();
event->accept();
}
if(event->button()==Qt::RightButton)
{
close();
}
}
void ShapeWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons()&Qt::LeftButton)
{
move(event->globalPos()-dragPosition);
event->accept();
}
}
void ShapeWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawPixmap(0,0,QPixmap("16.png"));
}