//...... H 文件
#ifndef SIMPLEFRAME_H
#define SIMPLEFRAME_H
#include
#include
#include "GeneratedFiles/ui_simpleframe.h"
class SimpleFrame : public QWidget
{
Q_OBJECT
public:
SimpleFrame(QWidget *parent = 0);
~SimpleFrame();
protected:
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
private:
QPoint mLastMousePosition;
bool mMoving;
private:
Ui::SimpleFrameClass ui;
};
#endif // SIMPLEFRAME_H
// ............. CPP 文件
#include "simpleframe.h"
SimpleFrame::SimpleFrame(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
setMouseTracking( true );
}
SimpleFrame::~SimpleFrame()
{
}
void SimpleFrame::mousePressEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton)
{
mMoving = true;
mLastMousePosition = event->globalPos();
}
}
void SimpleFrame::mouseMoveEvent(QMouseEvent* event)
{
if( event->buttons().testFlag(Qt::LeftButton) && mMoving)
{
this->move(this->pos() + (event->globalPos() - mLastMousePosition));
mLastMousePosition = event->globalPos();
}
}
void SimpleFrame::mouseReleaseEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton)
{
mMoving = false;
}
}