qt 界面去掉系统边框

该代码在Qt5框架编辑,使用该类时, 直接继承这个类就可以了。 实现了拖拽功能和关闭功能,如果需要放大缩小功能, 需自己实现。

1
#ifndef CUSTOMIZE_QWIDGET_H 2 #define CUSTOMIZE_QWIDGET_H 3 #include <QWidget> 4 #include <QMouseEvent> 5 6 class CustomizeQWidget : public QWidget 7 { 8 Q_OBJECT 9 public: 10 explicit CustomizeQWidget(QWidget *parent = 0); 11 ~CustomizeQWidget(); 12 public slots: 13 void on_button_close_clicked(); 14 private: 15 void paintEvent(QPaintEvent *); 16 void mousePressEvent(QMouseEvent *event); 17 void mouseMoveEvent(QMouseEvent *event); 18 private: 19 QPoint m_last_mouse_position; 20 }; 21 #endif // CUSTOMIZE_QWIDGET_H
 1 #include "customize_qwidget.h"
 2 #include <QStyleOption>
 3 #include <QPainter>
 4 #include <QBrush>
 5 
 6 CustomizeQWidget::CustomizeQWidget(QWidget *parent)
 7     : QWidget(parent)
 8 {
 9     this -> setWindowFlags(Qt::FramelessWindowHint);
10 }
11 
12 CustomizeQWidget::~CustomizeQWidget()
13 {
14 }
15 
16 void CustomizeQWidget::paintEvent(QPaintEvent *)
17 {
18     QStyleOption opt;
19     opt.init(this);
20     QPainter p(this);
21     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
22 }
23 
24 void CustomizeQWidget::mousePressEvent(QMouseEvent *event)
25 {
26     if(event->button() == Qt::LeftButton)
27     {
28         m_last_mouse_position = event->globalPos();
29     }
30 }
31 
32 void CustomizeQWidget::mouseMoveEvent(QMouseEvent *event)
33 {
34     if (!event->buttons().testFlag(Qt::LeftButton))
35             return;
36     const QPoint position = pos() + event->globalPos() - m_last_mouse_position; //the position of mainfrmae + (current_mouse_position - last_mouse_position)
37     move(position.x(), position.y());
38     m_last_mouse_position = event->globalPos();
39 }
40 
41 void CustomizeQWidget::on_button_close_clicked()
42 {
43     this->close();
44 }

 

posted @ 2019-10-23 12:28  HappyShadow  阅读(3324)  评论(2编辑  收藏  举报