qt 界面去掉系统边框

梦想与她 提交于 2019-12-02 02:17:58
 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 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!