qt 界面去掉系统边框
程序员文章站
2022-08-11 15:38:08
在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 }