Qt工作笔记-简单的画图工具

运行截图如下:

Qt工作笔记-简单的画图工具_第1张图片


代码如下:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 

class QMouseEvent;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

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

private:
    Ui::Widget *ui;
    QPixmap pix;
    QPoint lastPoint;
    QPoint endPoint;
    QPixmap tempPix;
    QPixmap tempPix2;
    bool isDrawing;
    bool isChange;

    int originalWidth;
    int originalHeight;

    bool winSizeIsChanged();
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include 
#include 
#include 
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("CSDN IT1995");
    pix=QPixmap(this->width(),this->height());
    pix.fill(Qt::white);
    isDrawing=false;
    isChange=false;
    originalWidth=this->width();
    originalHeight=this->height();

}

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

void Widget::paintEvent(QPaintEvent *event){
    Q_UNUSED(event);
    int x,y,w,h;
    x=lastPoint.x();
    y=lastPoint.y();
    w=endPoint.x()-x;
    h=endPoint.y()-y;

    QPainter painter(this);
    if(isDrawing){
        tempPix=pix;
        QPainter pp(&tempPix);
        if(ui->comboBox->currentIndex()==0){
            pp.setPen(Qt::red);
            pp.drawRect(x,y,w,h);
        }
        if(ui->comboBox->currentIndex()==1){
            pp.setPen(Qt::green);
            pp.drawEllipse(x,y,w,h);
        }
        if(ui->comboBox->currentIndex()==2){
            pp.setPen(Qt::blue);
            pp.drawRoundRect(x,y,w,h);
        }
        painter.drawPixmap(0,0,tempPix);
    }
    else{
        QPainter pp(&pix);
        if(ui->comboBox->currentIndex()==0&&isChange){
            pp.setPen(Qt::red);
            pp.drawRect(x,y,w,h);
        }
        if(ui->comboBox->currentIndex()==1&&isChange){
            pp.setPen(Qt::green);
            pp.drawEllipse(x,y,w,h);
        }
        if(ui->comboBox->currentIndex()==2&&isChange){
            pp.setPen(Qt::blue);
            pp.drawRoundRect(x,y,w,h);
        }
        painter.drawPixmap(0,0,pix);
    }

    if(winSizeIsChanged()){
        pix=QPixmap(this->width(),this->height());
        pix.fill(Qt::white);
        painter.drawPixmap(0,0,pix);
    }
    isChange=false;
}

bool Widget::winSizeIsChanged(){
    if(this->width()!=originalWidth||this->height()!=originalHeight){
        originalHeight=this->height();
        originalWidth=this->width();
        return true;
    }
    return false;
}

void Widget::mousePressEvent(QMouseEvent *event){
    if(event->button()==Qt::LeftButton){
        lastPoint=event->pos();
        endPoint=lastPoint;
        isDrawing=true;
    }

}

void Widget::mouseMoveEvent(QMouseEvent *event){
    if(event->buttons()&Qt::LeftButton){
        endPoint=event->pos();
        update();
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *event){
    if(event->button()==Qt::LeftButton){
        isChange=true;
        endPoint=event->pos();
        isDrawing=false;
        update();
    }
}

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

你可能感兴趣的:(C/C++,Qt,工作笔记)