资源简介

由于项目要涉及芯片检测的功能,需要选择特定区域,所以制作了个区域选择框,在QWidget上绘制区域框,可以放大缩小移动,可以是矩形,也可以是椭圆,注释非常完整。
需要详细介绍的可以参考我的博客https://blog.csdn.net/qq_40732350/article/details/103538593

资源截图

代码片段和文件信息

#include “DrawQWidget.h“
#include 
#include 
DrawQWidget::DrawQWidget(QWidget *parent) : QWidget(parent)
{
    setAttribute(Qt::WA_styledBackground);
    setstyleSheet(“background-color: rgb(0 0 0);“);
    grabKeyboard();
    setMouseTracking(true);

    m_difference_x                = 0;
    m_difference_y                = 0;
    draw_shap                     = DRAW_NO;
    painter                       = new QPainter(this);
    frame_pen                     = QPen(QColor(0174255)2);
    red_point_pen                 = QPen(QColor(25500)4);
    is_mouse_pressed              = false;
    timer_id = startTimer(20);


    rect_init_region();
    ellipse_init_region();
    /// 开启鼠标实时追踪
    setMouseTracking(true);

}

DrawQWidget::~DrawQWidget()
{
    killTimer(timer_id);
}


/* Event function
 *
 */
void DrawQWidget::timerEvent(QTimerEvent *)
{
    this->update();
}
void DrawQWidget::paintEvent(QPaintEvent *)
{
    painter->begin(this);
    painter->drawImage(QRectF(00width()height()) picture_image);
    switch (draw_shap) {
    case (DRAW_RECT)     :{
        painter->setPen(frame_pen);//绘制边框线
        painter->drawRect(QRect(rect_left rect_top rect_width rect_height));
        painter->setPen(red_point_pen);//绘制八个点
        painter->drawPoints(rect_polygon);
        }break;
    case (DRAW_ELLIPSE)  :{
        painter->setPen(frame_pen);//绘制边框线
        painter->drawEllipse(QRect(ellipse_left ellipse_top ellipse_width ellipse_height));
        painter->setPen(red_point_pen);//绘制四个点
        painter->drawPoints(ellipse_polygon);
        }break;
    case (DRAW_NO)       :break;
    }

    painter->end();
}

void DrawQWidget::mousePressEvent(QMouseEvent *event)
{
//    if (!is_start_draw) return;
    is_mouse_pressed = true;
}

void DrawQWidget::mouseMoveEvent(QMouseEvent *event)
{
//    if (!is_start_draw) return;
    new_mouse_pos = event->pos();
    if (is_mouse_pressed) {
        m_difference_x = new_mouse_pos.x() - old_mouse_pos.x();
        m_difference_y = new_mouse_pos.y() - old_mouse_pos.y();
        switch (draw_shap) {
        case (DRAW_RECT)     :rect_change_region();break;
        case (DRAW_ELLIPSE)  :ellipse_change_region();break;
        case (DRAW_NO)       :break;
        }
    }else{
        switch (draw_shap) {
        case (DRAW_RECT)     :rect_mouse_pos = rect_get_mouse_pos(new_mouse_pos.x() new_mouse_pos.y());break;
        case (DRAW_ELLIPSE)  :ellipse_mouse_pos = ellipse_get_mouse_pos(new_mouse_pos.x() new_mouse_pos.y());break;
        case (DRAW_NO)       :break;
        }

    }

    old_mouse_pos = new_mouse_pos;
}

void DrawQWidget::mouseReleaseEvent(QMouseEvent *event)
{
//    if (!is_start_draw) return;
    is_mouse_pressed = false;
}


/* Rect function
 *
 */
void DrawQWidget::rect_init_region()
{
    rect_left   = 100;
    rect_top    = 200;
    rect_width  = 101;
    rect_h

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-12-14 14:05  untitled\
     文件       10696  2019-12-14 13:44  untitled\DrawQWidget.cpp
     文件        4763  2019-12-14 13:42  untitled\DrawQWidget.h
     文件         175  2019-12-14 13:38  untitled\main.cpp
     文件         413  2019-12-14 13:41  untitled\untitled.pro
     文件       18492  2019-12-14 13:38  untitled\untitled.pro.user
     文件         830  2019-12-14 13:47  untitled\widget.cpp
     文件         404  2019-12-14 13:44  untitled\widget.h
     文件        1314  2019-12-14 13:44  untitled\widget.ui

评论

共有 条评论