当前位置:   article > 正文

QT 实现无标题栏窗口_qt 无标题栏窗体

qt 无标题栏窗体

最终的效果是这样的:
在这里插入图片描述

头文件:
class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

protected:
    void paintEvent(QPaintEvent *event) override;

    void mouseMoveEvent(QMouseEvent *event) override;

    virtual void mousePressEvent(QMouseEvent *event) override;
    virtual void mouseReleaseEvent(QMouseEvent *event) override;

private:
    Ui::Widget *ui;
    bool mIsHoveringOnCloseButton;
    bool mIsPressed;
    QPointF mPressedPos;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
CPP文件:
#include "Widget.h"
#include "ui_Widget.h"
#include <QPaintEvent>
#include <QPainter>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget),mIsHoveringOnCloseButton(false),mIsPressed(false)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    setMouseTracking(true);
}
void Widget::mouseReleaseEvent(QMouseEvent *event){
    mIsPressed = false;
    QWidget::mouseReleaseEvent(event);
}
void Widget::mousePressEvent(QMouseEvent *event){
    if(event){
        mIsPressed = true;
        mPressedPos = event->pos();
        if(mIsHoveringOnCloseButton){
            this->close();
            return;
        }
    }
    QWidget::mousePressEvent(event);
}
void Widget::mouseMoveEvent(QMouseEvent *event){
    if(event){
        if(mIsPressed){
            auto pos = event->globalPos();
            auto delta = pos - mPressedPos ;
            this->move(delta.toPoint()  );
        }
        else{
            auto pos = event->pos();
            const auto x = pos.x();
            const auto y = pos.y();
            if(x >= this->width() - 32 && x <= this->width() &&
                    y >= 0 && y <= 32){
                if(!mIsHoveringOnCloseButton){
                    mIsHoveringOnCloseButton = true;
                    update();
                }
            }
            else{
                if(mIsHoveringOnCloseButton){
                    mIsHoveringOnCloseButton = false;
                    update();
                }
            }
        }
    }
    QWidget::mouseMoveEvent(event);
}
void Widget::paintEvent(QPaintEvent *event) {
    if(event){
        QPainter painter(this);
        auto titleRect = event->rect();
        titleRect.setHeight(40);
        painter.setBrush(QColor("darkgray"));
        painter.setPen(Qt::NoPen);
        painter.drawRect(titleRect);

        auto closeButtonRect = event->rect();
        closeButtonRect.setLeft(width() - 32);
        closeButtonRect.setHeight(40);
        QFont font;
        font.setPixelSize(26);
        painter.setFont(font);
        if(mIsHoveringOnCloseButton)
            painter.setPen(QColor("magenta"));
        else
            painter.setPen(QColor("black"));
        painter.setBrush(Qt::NoBrush);
        painter.drawText(closeButtonRect, "✘");
    }
}
Widget::~Widget()
{
    delete ui;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/519593
推荐阅读
相关标签
  

闽ICP备14008679号