我们在日常开发的时候,几乎是很大的可能不使用系统自带的边框,但是我们去掉的话就没有窗口的拉伸效果,在网上找了几个demo都有bug,而且感觉写的很复杂,所以我重写了一个,如果有什么bug希望大家留言告知我。
#ifndef WIDGET_H #define WIDGET_H #include#include enum ClickType { CLICK_NULL, CLICK_TOP, CLICK_BOTTOM, CLICK_LEFT, CLICK_RIGHT, CLICK_BOTTOM_RIGHT, CLICK_TOP_RIGHT, CLICK_TOP_LETT, CLICK_BOTTOM_LETT, }; namespace Ui {class Widget;} class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); protected: void mouseMoveEvent(QMouseEvent *ev); void mousePressEvent(QMouseEvent *ev); void mouseReleaseEvent(QMouseEvent *ev); private slots: void on_pushButton_clicked(); private: Ui::Widget *ui; ClickType m_clickType = CLICK_NULL; bool m_bCanResize = false; bool m_bResizeIng = false; QSize m_oldSize; QPoint m_globalPoint; }; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h" //#include#define MOUSE_GAP 10 Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) { ui->setupUi(this); this->setMouseTracking(true);//开启鼠标追踪 this->setWindowFlags(Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint); this->setMinimumSize(100,100); m_oldSize = this->size(); m_globalPoint = this->pos(); } Widget::~Widget() { delete ui; } void Widget::mouseMoveEvent(QMouseEvent *ev) { static int rightTop; static int leftBottom; static int leftTop; if(!m_bResizeIng) { //右下角 if(ev->x() > this->width() - MOUSE_GAP && ev->y() > this->height() - MOUSE_GAP) { //qDebug() << "右下"; this->setCursor(Qt::SizeFDiagCursor); m_clickType = CLICK_BOTTOM_RIGHT; } //右上 else if(ev->x() > this->width() - MOUSE_GAP && ev->x() > MOUSE_GAP && ev->y() < MOUSE_GAP) { //qDebug() << "右上"; this->setCursor(Qt::SizeBDiagCursor); m_clickType = CLICK_TOP_RIGHT; } //左上 else if(ev->x() < MOUSE_GAP && ev->y() < MOUSE_GAP) { //qDebug() << "左上"; this->setCursor(Qt::SizeFDiagCursor); m_clickType = CLICK_TOP_LETT; } //左下 else if(ev->x() < MOUSE_GAP && ev->y() > this->height() - MOUSE_GAP) { //qDebug() << "左下"; this->setCursor(Qt::SizeBDiagCursor); m_clickType = CLICK_BOTTOM_LETT; } //左 else if(ev->x() < MOUSE_GAP) { //qDebug() << "左"; this->setCursor(Qt::SizeHorCursor); m_clickType = CLICK_LEFT; } //右边 else if(ev->x() > this->width() - MOUSE_GAP && ev->y() < this->height() - MOUSE_GAP) { //qDebug() << "右"; this->setCursor(Qt::SizeHorCursor); m_clickType = CLICK_RIGHT; } //上边 else if(ev->x() > MOUSE_GAP && ev->y() < MOUSE_GAP) { //qDebug() << "上"; this->setCursor(Qt::SizeVerCursor); m_clickType = CLICK_TOP; } //下边 else if(ev->y() > this->height() - MOUSE_GAP) { //qDebug() << "下"; this->setCursor(Qt::SizeVerCursor); m_clickType = CLICK_BOTTOM; } else { //qDebug() << "其他"; this->setCursor(Qt::ArrowCursor); m_clickType = CLICK_NULL; } } if(m_bCanResize && m_clickType != CLICK_NULL) { //qDebug() << "可以改变尺寸"; if(m_clickType == CLICK_RIGHT) { if(m_globalPoint.x() < ev->globalPos().x()) { m_bResizeIng = true; //qDebug() << "向右拉大"; this->resize(m_oldSize.width() + (ev->globalX() - m_globalPoint.x()),this->height()); } else if(ev->globalPos().x() < m_globalPoint.x()) { m_bResizeIng = true; //qDebug() << "向右拉小"; this->resize(m_oldSize.width() - (m_globalPoint.x() - ev->globalX()),this->height()); } } else if(m_clickType == CLICK_BOTTOM) { if(ev->globalY() > m_globalPoint.y()) { //qDebug() << "向下拉大"; m_bResizeIng = true; this->resize(this->width(),this->height() + ev->globalY() - m_globalPoint.y()); } else if(ev->globalY() < m_globalPoint.y()) { //qDebug() << "向下拉小"; m_bResizeIng = true; this->resize(this->width(),this->height() - (m_globalPoint.y() - ev->globalY())); } } else if(m_clickType == CLICK_LEFT) { if(m_globalPoint.x() > ev->globalX() && this->x() > 0) { m_bResizeIng = true; //qDebug() << "向左拉大"; this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height()); this->move(this->x() - (m_globalPoint.x() - ev->globalPos().x()),this->y()); } else if(m_globalPoint.x() < ev->globalX()) { if(this->width() != this->minimumWidth()) { m_bResizeIng = true; //qDebug() << "向左拉小"; this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height()); this->move(rightTop - this->width(),this->y()); } } } else if(m_clickType == CLICK_TOP) { if(m_globalPoint.y() > ev->globalY() && ev->globalY() > 0) { m_bResizeIng = true; //qDebug() << "向上拉大"; this->resize(this->width(),this->height() + (m_globalPoint.y() - ev->globalY())); this->move(this->x(),this->y() - (m_globalPoint.y() - ev->globalY())); } else if(m_globalPoint.y() < ev->globalY()) { m_bResizeIng = true; //qDebug() << "向上拉小"; this->resize(this->width(),this->height() - (ev->globalY() - m_globalPoint.y())); this->move(this->x(),leftBottom - this->height()); } } else if(m_clickType == CLICK_BOTTOM_RIGHT) { if(ev->globalX() > m_globalPoint.x() || ev->globalY() > m_globalPoint.y()) { m_bResizeIng = true; this->resize(m_oldSize.width() + (ev->globalX() - m_globalPoint.x()),this->height()); this->resize(this->width(),this->height() + ev->globalY() - m_globalPoint.y()); //qDebug() << "右下拉大"; } else if(ev->globalX() < m_globalPoint.x() || ev->globalY() < m_globalPoint.y()) { m_bResizeIng = true; //qDebug() << "右下拉小"; this->resize(m_oldSize.width() - (m_globalPoint.x() - ev->globalX()),this->height()); this->resize(this->width(),this->height() - (m_globalPoint.y() - ev->globalY())); } } else if(m_clickType == CLICK_TOP_RIGHT) { if(ev->globalX() > m_globalPoint.x() || ev->globalY() < m_globalPoint.y()) { m_bResizeIng = true; //qDebug() << "右上拉大"; this->resize(this->width(),this->height() + (m_globalPoint.y() - ev->globalY())); this->resize(m_oldSize.width() + (ev->globalX() - m_globalPoint.x()),this->height()); this->move(leftTop,this->y() - (m_globalPoint.y() - ev->globalY())); } else if(ev->globalX() < m_globalPoint.x() || ev->globalY() > m_globalPoint.y()) { m_bResizeIng = true; //qDebug() << "右上拉小"; this->resize(this->width(),this->height() - (ev->globalY() - m_globalPoint.y())); this->resize(m_oldSize.width() - (m_globalPoint.x() - ev->globalX()),this->height()); this->move(leftTop,leftBottom - this->height()); } } else if(m_clickType == CLICK_TOP_LETT) { if(ev->globalX() < m_globalPoint.x() || ev->globalY() < m_globalPoint.y()) { m_bResizeIng = true; //qDebug() << "左上拉大"; this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height()); this->move(this->x() - (m_globalPoint.x() - ev->globalPos().x()),this->y()); this->resize(this->width(),this->height() + (m_globalPoint.y() - ev->globalY())); this->move(this->x(),this->y() - (m_globalPoint.y() - ev->globalY())); } else if(ev->globalX() > m_globalPoint.x() || ev->globalY() > m_globalPoint.y()) { m_bResizeIng = true; //qDebug() << "左上拉小"; this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height()); this->move(rightTop - this->width(),this->y()); this->resize(this->width(),this->height() - (ev->globalY() - m_globalPoint.y())); this->move(this->x(),leftBottom - this->height()); } } else if(m_clickType == CLICK_BOTTOM_LETT) { if(ev->globalX() < m_globalPoint.x() || ev->globalY() > m_globalPoint.y()) { m_bResizeIng = true; //qDebug() << "左下拉大"; this->resize(this->width(),this->height() + ev->globalY() - m_globalPoint.y()); this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height()); this->move(this->x() - (m_globalPoint.x() - ev->globalPos().x()),this->y()); } else if(ev->globalX() > m_globalPoint.x() || ev->globalY() < m_globalPoint.y()) { m_bResizeIng = true; //qDebug() << "左下拉小"; this->resize(this->width(),this->height() - (m_globalPoint.y() - ev->globalY())); this->resize(m_oldSize.width() + m_globalPoint.x() - ev->globalX(),this->height()); this->move(rightTop - this->width(),this->y()); } } } leftTop = this->x(); leftBottom = this->y() + this->height(); rightTop = this->x() + this->width(); m_oldSize = this->size(); m_globalPoint = ev->globalPos(); } void Widget::mousePressEvent(QMouseEvent *ev) { if(ev->buttons() == Qt::LeftButton && m_clickType != CLICK_NULL) { m_bCanResize = true; } } void Widget::mouseReleaseEvent(QMouseEvent *ev) { this->setCursor(Qt::ArrowCursor); m_clickType = CLICK_NULL; m_bCanResize = false; m_bResizeIng = false; } void Widget::on_pushButton_clicked() { QApplication::exit(); }
PS:
1、窗口最好限定最大最小尺寸。
2、向左拉伸的时候,窗口最右边会有抖动,但是我发现市面上支持拉伸的软件向左拉伸时都 会有这个问题,而且我打印了坐标发现点位并没有变化,所以这应该不是程序的问题。



