我是靠谱客的博主 感动草丛,这篇文章主要介绍QT Qwidget设置窗口无边框,并且可拖动,现在分享给大家,希望可以做个参考。

首先设置无边框

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

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

复制代码
1
2
1 //设置无边框 2 this->setWindowFlags(Qt::FramelessWindowHint);

然后是拖动功能

.h文件设置

复制代码
1
2
3
4
5
6
7
8
9
1 #include <QMouseEvent> 2 3 protected: 4 void mouseMoveEvent(QMouseEvent *event); 5 void mousePressEvent(QMouseEvent *event); 6 void mouseReleaseEvent(QMouseEvent *event); 7 private: 8 QPoint mousePoint; 9 bool mouse_press;

.cpp文件设置

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1 void Softdog::mousePressEvent(QMouseEvent *event) 2 { 3 4 if( (event->button() == Qt::LeftButton) ){ 5 mouse_press = true; 6 mousePoint = event->globalPos() - this->pos(); 7 // event->accept(); 8 } 9 else if(event->button() == Qt::RightButton){ 10 //如果是右键 11 this->close(); 12 13 } 14 } 15 void Softdog::mouseMoveEvent(QMouseEvent *event) 16 { 17 18 19 // if(event->buttons() == Qt::LeftButton){ //如果这里写这行代码,拖动会有点问题 20 if(mouse_press){ 21 move(event->globalPos() - mousePoint); 22 // event->accept(); 23 } 24 } 25 void Softdog::mouseReleaseEvent(QMouseEvent *event) 26 { 27 mouse_press = false; 28 }

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

最后

以上就是感动草丛最近收集整理的关于QT Qwidget设置窗口无边框,并且可拖动的全部内容,更多相关QT内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部