Qt 5 重写QLabel实现在QLabel上勾画不规则闭合图形的功能

本文档详细介绍了如何在Qt 5环境下重写QLabel类,通过FPoint和PLabel两个组件,实现QLabel上绘制不规则闭合图形的功能。涉及到头文件(.h)和源文件(.cpp)的编写,提供了具体代码示例。
摘要由CSDN通过智能技术生成

FPoint

.h

#ifndef FPOINT_H
#define FPOINT_H
class FPoint
{
public:
    FPoint(float x, float y);

    float x, y;

    FPoint operator+(const FPoint& b) {
        return FPoint(x + b.x, y + b.y);
    }

    FPoint operator-(const FPoint& b) {
        return FPoint(x - b.x, y - b.y);
    }
};
#endif // FPOINT_H

.cpp

#include "fpoint.h"

FPoint::FPoint(float x, float y) {
    this.x = x;
    this.y = y;
}

PLabel

.h

#include <QLabel>
#include <QPoint>
#include <QMouseEvent>
#include <QPainter>
#include <QWidget>
#include <iostream>
#include <opencv2/core.hpp>
#include <vector>
#include "fpoint.h"

class RectLabel : public QLabel {
    Q_OBJECT

public:
    RectLabel(QWidget *parent = nullptr);

protected:
    void paintEvent(QPaintEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

    int doubleCompare(double x);
    bool between2Points(FPoint &a1, FPoint &a2, FPoint &p);
    double cross(FPoint a, FPoint b);
    double distance2Points(FPoint p1, FPoint p2);
    void drawLine(QPainter &paint, FPoint &po1, FPoint&po2);

signals:
    void sendRect(cv::Rect2f rt);

private:
    std::vector<FPoint> points;
    int selectIdx = 0;
    bool stateSelect = false;

    float startX = 0, startY = 0;
    float endX = 0, endY = 0;
};

.CPP

#pragma execution_character_set("utf-8")
#include "rect
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值