1. 程式人生 > >Qt之去除視窗的標題欄、通過滑鼠移動視窗

Qt之去除視窗的標題欄、通過滑鼠移動視窗

------------ main.c -------------------
#include<QApplication>
#include<QWidget>
#include<QDebug>
#include"mywidget.h"
int main(int argc,char **argv)
{
    QApplication app(argc,argv);
    //功能新的需求:不再使用框架提供的視窗,自定義一個視窗;
    //派生類;
    MyWidget w;  //MyWidget類繼承於基類QWidget類;
    w.show();
    return
app.exec(); } ------------ MyWidget.h ------------------- #ifndef MYWIDGET_H #define MYWIDGET_H #include<QMouseEvent> #include <QWidget> class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); void mousePressEvent(QMouseEvent *ev); void
mouseMoveEvent(QMouseEvent *ev); //移動視窗 void mouseReleaseEvent(QMouseEvent *ev); //釋放視窗 //滑鼠原始位置 QPoint posMouseOrigin; signals: public slots: }; #endif // MYWIDGET_H ------------ MyWidget.cpp ------------------- #include "mywidget.h" #include<QDebug> #include<QWidget>
#include<QPoint> #include<QMouseEvent> MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { this->setWindowFlags(Qt::FramelessWindowHint); //去掉標題欄; } //獲取滑鼠的按下操作 void MyWidget::mousePressEvent(QMouseEvent *ev) { this->posMouseOrigin = QCursor::pos(); //cursor是一個游標類; } //獲得滑鼠移動的操作 void MyWidget::mouseMoveEvent(QMouseEvent *ev) { QPoint ptMouseNow = QCursor::pos(); QPoint ptDelta = ptMouseNow - this->posMouseOrigin; move(this->pos() + ptDelta); posMouseOrigin = ptMouseNow; } //獲得滑鼠的釋放操作; void MyWidget::mouseReleaseEvent(QMouseEvent *ev) { }