【QT】Qwidget设置窗口无边框,并且可拖动

首先设置无边框

在这里一定要注意的是函数名称是setWindowFlags而不是setWindowFlag,

一字之差可能让你头疼半天为什么边框还在,没效果

//设置无边框
this->setWindowFlags(Qt::FramelessWindowHint);

然后是拖动功能
.h文件设置

#include 



protected:
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
    QPoint mousePoint;
    bool mouse_press;

.cpp文件设置

void Softdog::mousePressEvent(QMouseEvent *event)
{

    if( (event->button() == Qt::LeftButton) ){
        mouse_press = true;
        mousePoint = event->globalPos() - this->pos();
//        event->accept();
    }
    else if(event->button() == Qt::RightButton){
        //如果是右键
        this->close();

    }
}
void Softdog::mouseMoveEvent(QMouseEvent *event)
{


//    if(event->buttons() == Qt::LeftButton){  //如果这里写这行代码,拖动会有点问题
    if(mouse_press){
        move(event->globalPos() - mousePoint);
//        event->accept();
    }
}
void Softdog::mouseReleaseEvent(QMouseEvent *event)
{
    mouse_press = false;
}

 

你可能感兴趣的:(qt)