Qt使用記錄——去掉標題欄,窗口可移動


轉載來自:https://www.2cto.com/kf/201302/191602.html

在用QT編寫界面時,去掉標題欄方法比較簡單,就一行代碼

this->setWindowFlags(Qt::FramelessWindowHint);
去掉以后又發現一個問題,就是不能移動窗口了,於是我就重寫了三個鼠標事件,大致代碼如下
.h文件的代碼:
 
#include <QMouseEvent>
protected:
    void mousePressEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
private:
    QPoint last;

 

.cpp文件的代碼
 
//可以在構造函數中初始一下last變量用其成員函數setX,setY就是了
//接下來就是對三個鼠標事件的重寫
void MainWindow::mousePressEvent(QMouseEvent *e)
{
    last = e->globalPos();
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    last = e->globalPos();
    move(x()+dx, y()+dy);
}
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    move(x()+dx, y()+dy);
} 
復制過去用的時候記得把類名改掉哦~
這樣就OK了,去掉窗口標題欄后還能拖動窗體


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM