创建一个矩形,当鼠标进入这个矩形的时候,这个矩形边线变色,且鼠标变成手型

发布于:2024-07-18 ⋅ 阅读:(57) ⋅ 点赞:(0)

1.概要

创建一个矩形,当鼠标进入这个矩形的时候,这个矩形边线变色,且鼠标变成手型

2.代码

#include <QApplication>
#include "customRectWidget.h"
/*
 * qt 6.7版, 创建一个矩形,当鼠标进入这个矩形的时候,这个矩形边线变色,
 * 且鼠标变成手型,且这时候按下鼠标,鼠标可以上下拖动这个矩形,
 * 当鼠标松开的时候停止移动,但鼠标离开矩形的时候,矩形颜色恢复,完整代码举例;
 * 强调一下,鼠标进入矩形内,颜色变化,鼠标变成手型,不是鼠标进入矩形所在的控件就有上述变化。
*/
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    CustomRectWidget widget;
    widget.show();
    return app.exec();
}
#ifndef CUSTOMRECTWIDGET_H
#define CUSTOMRECTWIDGET_H

#include <QWidget>
#include <QMouseEvent>

class CustomRectWidget : public QWidget {
    Q_OBJECT

public:
    explicit CustomRectWidget(QWidget *parent = nullptr);

protected:
    void paintEvent(QPaintEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void enterEvent(QEnterEvent *event) override;
    void leaveEvent(QEvent *event) override;
    void setIsChange(const QPoint &mousePos);
private:
    QRect rect;
    bool dragging;
    QPoint dragStartPoint;
    QColor hoverColor;
    QColor normalColor;
    bool isChange;

    bool isMouseInsideRect(const QPoint &mousePos) const;
};

#endif // CUSTOMRECTWIDGET_H

 

#include "customRectWidget.h"
#include <QPainter>
#include <QCursor>
#include <QDebug>

CustomRectWidget::CustomRectWidget(QWidget *parent)
    : QWidget(parent), rect(50, 50, 200, 100), dragging(false), hoverColor(Qt::red), normalColor(Qt::black) {
    setFixedSize(300, 300);
    // 启用鼠标追踪,以便在鼠标没有按下时也能接收mouseMoveEvent
    setMouseTracking(true);
}

void CustomRectWidget::setIsChange(const QPoint &mousePos){
    bool value = isMouseInsideRect(mousePos);
    if(value==isChange){
        return;
    }
    update();
    isChange = value;
}

void CustomRectWidget::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event)
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QColor borderColor = normalColor;
    if (isMouseInsideRect(mapFromGlobal(QCursor::pos()))) {
        borderColor = hoverColor;
    }

    painter.setPen(QPen(borderColor, 3));
    painter.setBrush(Qt::white);
    painter.drawRect(rect);
}

void CustomRectWidget::mousePressEvent(QMouseEvent *event) {
    setIsChange(event->pos());
    if (isMouseInsideRect(event->pos())) {
        dragging = true;
        dragStartPoint = event->pos();
    }
}

void CustomRectWidget::mouseMoveEvent(QMouseEvent *event) {
    setIsChange(event->pos());
    if (dragging && isMouseInsideRect(dragStartPoint)) {
        int dy = event->pos().y() - dragStartPoint.y();
        rect.moveTop(rect.top() + dy);
        dragStartPoint = event->pos();
        update(); // Trigger repaint with new position
    }
}

void CustomRectWidget::mouseReleaseEvent(QMouseEvent *event) {
    Q_UNUSED(event)
    dragging = false;
}

void CustomRectWidget::enterEvent(QEnterEvent *event) {
    Q_UNUSED(event)
    // We manually set the cursor in the mouseMoveEvent based on the rect position
}

void CustomRectWidget::leaveEvent(QEvent *event) {
    Q_UNUSED(event)
    // Cursor will be reset to default when leaving the widget
}

bool CustomRectWidget::isMouseInsideRect(const QPoint &mousePos) const {
    bool ret = rect.contains(mousePos);
    if(ret) {
        qDebug()<<"in";
    }else{
        qDebug()<<"not in";
    }
    return ret;
}

 3.运行结果


网站公告

今日签到

点亮在社区的每一天
去签到