.h
#ifndef WIDGET_H
#define WIDGET_H
#include
//#include
//#include
#include
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
protected:
//bool nativeEvent(const QByteArray &eventType, void *message, long *result);
private:
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *);
void mousePressEvent(QMouseEvent *event);
bool mouseLeftPressChangeSize; //鼠标左键按下
bool mouseLeftPressMove; //鼠标左键按下
QPoint pressPoint;
int Temp_Flag;
};
#endif // WIDGET_H
.cpp
#include "widget.h"
#include "QDebug"
#include
#include
#include
#include
int g_minW = 50;
int g_minH = 50;
int g_borderRadius=20;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setMouseTracking(true);
// 背景透明
setAttribute(Qt::WA_TranslucentBackground, true);
// 设置无边框
setWindowFlags(windowFlags() |Qt::FramelessWindowHint);
QWidget* widget = new QWidget(this);
widget->setObjectName("widget");
widget->setStyleSheet("QWidget#widget{background-color:white;border-radius:5px;}");
QHBoxLayout* hbox = new QHBoxLayout(this);
hbox->addWidget(widget);
this->setLayout(hbox);
QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect(widget);
shadow->setColor(Qt::gray);
shadow->setOffset(0, 0);
shadow->setBlurRadius(g_borderRadius);
widget->setGraphicsEffect(shadow);
resize(500,500);
mouseLeftPressChangeSize = false;
mouseLeftPressMove = false;
}
Widget::~Widget()
{
}
void Widget::mouseMoveEvent(QMouseEvent * event)
{
QRect rectThis = this->geometry();
QPoint currentPoint = QCursor::pos();//获取鼠标的绝对位置
int Temp_x = currentPoint.x() - rectThis.x();
int Temp_y = currentPoint.y() - rectThis.y();
if( mouseLeftPressChangeSize == false ){
int m_border = 5;
this->setCursor(Qt::ArrowCursor);
int Temp_FlagRight = 0,Temp_FlagBotton = 0;
//下
if( qAbs( Temp_y - this->height() ) < m_border ){
Temp_FlagBotton = 1;
Temp_Flag = 2;
this->setCursor(Qt::SizeVerCursor);
}
//右
if( qAbs( Temp_x - this->width() ) < m_border ){
Temp_FlagRight = 1;
Temp_Flag = 4;
this->setCursor(Qt::SizeHorCursor);
}
//右下
if( Temp_FlagBotton && Temp_FlagRight ){
this->setCursor(Qt::SizeFDiagCursor);
Temp_Flag = 8;
}
}
else{
if ((event->buttons() == Qt::LeftButton) )
{
if(Temp_x < g_minW || Temp_y < g_minH)
return;
switch (Temp_Flag) {
case 2://下
move(rectThis.x(),rectThis.y());
this->setFixedHeight( Temp_y );
break;
case 4://右
move(rectThis.x(),rectThis.y());
this->setFixedWidth( Temp_x );
break;
case 8://右下
move(rectThis.x(),rectThis.y());
this->setFixedSize( Temp_x ,Temp_y );
break;
default:
break;
}
}
}
if (mouseLeftPressMove && (event->buttons() == Qt::LeftButton))
{
move(event->globalPos() - pressPoint);
}
}
void Widget::mouseReleaseEvent(QMouseEvent *)
{
mouseLeftPressMove = false;
mouseLeftPressChangeSize = false;
}
void Widget::mousePressEvent(QMouseEvent * event)
{
QRect rectThis = this->geometry(); //界面Rect
QPoint currentPoint = QCursor::pos();//获取鼠标的绝对位置
int m_border = 5;//焦点范围
int Temp_FlagRight = 0,Temp_FlagBotton = 0;
if (event->buttons() == Qt::LeftButton) {
//下
if( qAbs( currentPoint.y() - rectThis.y() - this->height() ) < m_border ){
Temp_FlagBotton = 1;
mouseLeftPressChangeSize = true;
}
//右
if( qAbs( currentPoint.x() - rectThis.x() - this->width() ) < m_border ){
Temp_FlagRight = 1;
mouseLeftPressChangeSize = true;
}
//右下
if( Temp_FlagBotton && Temp_FlagRight ){
mouseLeftPressChangeSize = true;
}
if(!mouseLeftPressChangeSize &&
currentPoint.x() > this->x() && currentPoint.x() < this->x() + width() &&
currentPoint.y() > this->y() && currentPoint.y() < this->y() + height()){
mouseLeftPressMove = true;
pressPoint = event->globalPos() - pos();
}
}
}