1. 程式人生 > 其它 >QT筆記——QPainter繪製動態矩形

QT筆記——QPainter繪製動態矩形

技術標籤:QT學習筆記qt

繪製動態矩形
drawRects.h

#ifndef DRAWRECTS_H
#define DRAWRECTS_H

#include <QWidget>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QVector>
#include <QRect>
#include <QPoint>
namespace Ui {
class DrawRects;
}

class DrawRects
: public QWidget { Q_OBJECT public: explicit DrawRects(QWidget *parent = nullptr); ~DrawRects(); void drawRects(QPainter *painter); //繪製多個矩形 protected: void paintEvent(QPaintEvent *); //繪製 void mousePressEvent(QMouseEvent *); //按下
void mouseMoveEvent(QMouseEvent *); //移動 void mouseReleaseEvent(QMouseEvent *); //鬆開 private: Ui::DrawRects *ui; bool drawIsRects; //是否在繪製多個矩形 bool mouseIsMove; //是否在移動 bool mouseIsClickKeep;
//是否滑鼠保持移動(滑鼠沒鬆開) bool drawIsFinshed; //繪圖是否完成 QVector<QPoint> rectPoint; //儲存繪製矩形的點 QPoint movePoint; //儲存移動的點(滑鼠點選未鬆開) }; #endif // DRAWRECTS_H

drawRects.cpp

#include "drawrects.h"
#include "ui_drawrects.h"

DrawRects::DrawRects(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DrawRects)
{
    ui->setupUi(this);

    drawIsRects = false;
    mouseIsMove = false;
    mouseIsClickKeep = false;
    drawIsFinshed = false;
}

DrawRects::~DrawRects()
{
    delete ui;
}

void DrawRects::drawRects(QPainter *painter)
{
    drawIsRects = true;
    if(drawIsRects)
    {
        //記錄矩形的個數
        int rectCount = 0;
        if(rectPoint.size() % 2 == 0 )
        {
            rectCount = rectPoint.size() / 2;
        }
        else
        {
            rectCount = (rectPoint.size() - 1) / 2;
        }

        //繪製未完成的(移動狀態)矩形
        if(mouseIsMove && mouseIsClickKeep)
        {
            int width = movePoint.x() - rectPoint[rectCount * 2].x();
            int height = movePoint.y() - rectPoint[rectCount * 2].y();
            painter->drawRect(QRect(rectPoint[rectCount * 2].x(), rectPoint[rectCount * 2].y(), width, height));
        }
        //繪製完成的矩形
        for(int i = 0; i < rectCount; i++)
        {
            int width = rectPoint[i * 2 + 1].x() - rectPoint[i * 2].x();
            int height = rectPoint[i * 2 + 1].y() - rectPoint[i * 2].y();
            painter->drawRect(QRect(rectPoint[i * 2].x(), rectPoint[i * 2].y(), width, height));
        }
    }


}

void DrawRects::paintEvent(QPaintEvent *)
{
   QPainter painter(this);
   drawRects(&painter);
}

void DrawRects::mousePressEvent(QMouseEvent *e)
{
    if(drawIsRects)
    {
        //滑鼠是否是保持按下不變的狀態
        if(!mouseIsClickKeep)
        {
            mouseIsClickKeep = true;
            //將點選的點存放在容器中
            rectPoint.push_back(e->pos());
            drawIsFinshed = false;
            this->update();
        }
    }
}

void DrawRects::mouseMoveEvent(QMouseEvent *e)
{
    if(drawIsRects && mouseIsClickKeep)
    {
        //記錄移動狀態時候的點
        movePoint = e->pos();
        mouseIsMove = true;
        drawIsFinshed = false;
        this->update();
    }
}

void DrawRects::mouseReleaseEvent(QMouseEvent *e)
{
    if(drawIsRects)
    {
        mouseIsClickKeep = false;
        mouseIsMove = false;
        drawIsFinshed = true;
        rectPoint.push_back(e->pos());
        this->update();
    }
}

執行結果:
繪製多個動態矩形