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;
};
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;
}

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值