.h
protected:
void mouseMoveEvent (QMouseEvent* event);
void mousePressEvent (QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent*);
private:
QPoint mousePosition;
bool isMousePressed;
//标题栏的长度
const static int pos_min_x = 0;
const static int pos_max_x = 800-40;
const static int pos_min_y = 0;
const static int pos_max_y = 20;
.cpp
void MyMsgTip::mouseMoveEvent(QMouseEvent *event)
{
if (isMousePressed == true )
{
QPoint movePot = event->globalPos() - mousePosition;
move(movePot);
}
}
void MyMsgTip::mousePressEvent(QMouseEvent *event)
{
mousePosition = event->pos();
//只对标题栏范围内的鼠标事件进行处理
if (mousePosition.x()<=pos_min_x)
return;
if ( mousePosition.x()>=pos_max_x)
return;
if (mousePosition.y()<=pos_min_y )
return;
if (mousePosition.y()>=pos_max_y)
return;
isMousePressed = true;
}
void MyMsgTip::mouseReleaseEvent(QMouseEvent *)
{
isMousePressed=false;
}