Qt系列--无边框,重写事件实现功能

Qt取消边框,处理常见的事件

1.处理移动窗口和拉伸窗口机制

class MyMidArea : public QWidget
{
    Q_OBJECT
public:
    explicit MyMidArea(QList<QPair<int,QString>> id_name,QList<QList<Line>> data,QWidget *parent = nullptr);
signals:

protected:
    // 创建圆角遮罩
    QRegion createRoundedMask(const QSize &size, int radius) {
        QRegion mask;
        mask = QRegion(0, 0, size.width(), size.height(), QRegion::Rectangle);
        mask -= QRegion(0, 0, radius, radius, QRegion::Ellipse);
        mask -= QRegion(0, size.height() - radius, radius, radius, QRegion::Ellipse);
        mask -= QRegion(size.width() - radius, 0, radius, radius, QRegion::Ellipse);
        mask -= QRegion(size.width() - radius, size.height() - radius, radius, radius, QRegion::Ellipse);
        return mask;
    }
private:
    //QPushButton *m_p_btn_left;
    //QPushButton *m_p_btn_right;
    QToolButton *m_p_btn_left;
    QToolButton *m_p_btn_right;
    myCheckWidget *m_checkWidget;
    QList<QPair<int,QString>>    m_id_name;//id和名字
    QList<QList<Line>>          m_data;
    qint8 m_pos = -1;          //记录显示第几个模型的数据

protected:
    QPoint m_dragStartPosition; // 记录拖动起始位置
    bool m_isResizing = false; // 是否正在调整大小
    bool m_isMove = false;//是否移动
    int m_resizeMargin = 5; // 调整大小边界范围

    void mousePressEvent(QMouseEvent *event) override {
        if (event->button() == Qt::LeftButton) {
            if (event->pos().x() < m_resizeMargin || event->pos().x() > width() - m_resizeMargin ||
                event->pos().y() < m_resizeMargin || event->pos().y() > height() - m_resizeMargin) {
                // 如果点击在调整大小的边界内,进入调整大小模式
                m_isResizing = true;
            } else {
                // 否则,进入拖动模式
                m_isMove=true;
                m_dragStartPosition = event->pos();
            }
        }
    }

    void mouseMoveEvent(QMouseEvent *event) override {
        if (m_isResizing) {
            // 调整大小
            resize(event->globalX() - geometry().left(), event->globalY() - geometry().top());
        } else {
            // 拖动
            QPoint delta = event->pos() - m_dragStartPosition;
            move(mapToGlobal(delta));
        }
    }

    void mouseReleaseEvent(QMouseEvent *event) override {
        m_isResizing = false;
        if(!m_isMove)//没有移动直接关闭窗口
        {
            this->close();
        }
        m_isMove = false;
    }
};

你可能感兴趣的:(QT,qt)