Qt实现没有标题栏的窗体移动

1820阅读 0评论2020-12-04 walterpeng
分类:C/C++

//...... 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; 
    } 

上一篇:C#进程间通信--API传递参数(SendMessage)
下一篇:进程间通信---共享内存