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 }
原文地址:https://www.cnblogs.com/devil-shadow/p/11725622.html
时间: 2024-11-08 10:45:59