Qt绘画板制作代码

Qt绘画板制作代码_第1张图片

 头文件:

drawboard.h

#ifndef DRAWBOARD_H
#define DRAWBOARD_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

namespace Ui {
class DrawBoard;
}

class DrawBoard : public QWidget
{
    Q_OBJECT

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

    QPoint start;
    QPoint end;
    QPixmap *pix;
    QPixmap *pix1;
    QString style = "曲线";
    QColor color = Qt::black;
    int width = 2;
    bool isEraser = false;

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

private:
    Ui::DrawBoard *ui;
};

#endif // DRAWBOARD_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

源文件:

drawboard.cpp

#include "drawboard.h"
#include "ui_drawboard.h"
#include 

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

    //绘制的直线画再图片
    pix = new QPixmap(1600,1200);
    pix ->fill();
    pix1 = new QPixmap(1600,1200);
    pix1 ->fill();


}

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

void DrawBoard::paintEvent(QPaintEvent *event)
{
    //创建一个画家对象进行绘制,绘制控件为一条直线
    //
    QPainter p(this);
    //绘制静态直线
//    p.drawLine(QPoint(0,0),QPoint(300,300));
    //绘制动态直线
//    p.drawLine(start,end);
    //将画好直线的图片绘制到draw Board
    p.drawPixmap(QPoint(0,0),*pix);
}

void DrawBoard::mousePressEvent(QMouseEvent *event)
{
    start = event->pos();
    *pix1 = *pix;
}

void DrawBoard::mouseMoveEvent(QMouseEvent *event)
{
    end = event->pos();//获取鼠标释放时的坐标
    if(isEraser==true)
    {
        QPainter p(pix);
        p.setPen(QPen(QBrush(Qt::white),6));
        p.drawLine(start,end);
        start = end;
    }
    else
    {
        //当选中为曲线时,不需要进行pix替换
        if(style!="曲线")
        *pix = *pix1;

        //将直线绘制到图片上
        QPainter p(pix);

        //设置画笔的属性
        p.setPen(QPen(QBrush(color),width));

        if(style == "直线")
        {
            p.drawLine(start,end);
        }
        else if(style=="曲线")
        {
            p.drawLine(start,end);
            start = end;
        }
        else if(style=="矩形")
        {
            p.drawRect(QRect(start,end));
        }
        else if(style=="椭圆")
        {
            p.drawEllipse(QRect(start,end));
        }
    }
    update();//间接调用paintEvent方法
}

//void DrawBoard::mouseReleaseEvent(QMouseEvent *event)
//{
//    end = event->pos();//获取鼠标释放时的坐标
//    //将直线绘制到图片上
//    QPainter p(pix);
//    p.drawLine(start,end);
//    update();//间接调用paintEvent方法
//}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "drawboard.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    DrawBoard* board = new DrawBoard();
    this->resize(1600,1200);
    this->setCentralWidget(board);

    DrawBoard *localDrawBoard = new DrawBoard();
    this->setCentralWidget(localDrawBoard);

    //画笔类型功能
    QComboBox *combo = new QComboBox();
    combo->addItem("直线");
    combo->addItem("曲线");
    combo->addItem("矩形");
    combo->addItem("椭圆");
    ui->mainToolBar->addWidget(new QLabel("绘制类型:"));
    ui->mainToolBar->addWidget(combo);

    connect(combo,QComboBox::currentTextChanged,[=](QString style){
        localDrawBoard->style = style;
    });

    //画笔宽度
    ui->mainToolBar->addWidget(new QLabel("画笔宽度:"));
     QComboBox *comboWidth = new QComboBox();
     int i = 1;
     while(i<=30)
     {
         comboWidth->addItem(QString::number(i));
         i++;
     }
    ui->mainToolBar->addWidget(comboWidth);
    connect(comboWidth,QComboBox::currentTextChanged,[=](QString width){
        localDrawBoard->width = width.toInt();
    });

    //画笔颜色
    QToolButton *localQToolButton = new QToolButton();
    localQToolButton->setText("颜色");
    ui->mainToolBar->addSeparator();
    ui->mainToolBar->addWidget(localQToolButton);

    connect(localQToolButton,QToolButton::clicked,[=](){
       QColor color = QColorDialog::getColor();

       //扩展:颜色按钮背景为选中的颜色
       localDrawBoard->color = color;
       localQToolButton->setStyleSheet(QString("background-color: rgb(%1,%2,%3);").arg(color.red()).arg(color.green()).arg(color.blue()));
    });


    //橡皮
    QToolButton *eraserBtn = new QToolButton();
    eraserBtn->setText("橡皮");
    eraserBtn->setCheckable(true);
    ui->mainToolBar->addSeparator();
    ui->mainToolBar->addWidget(eraserBtn);

    connect(eraserBtn,QToolButton::clicked,[=](bool checked){
        localDrawBoard->isEraser = checked;
    });

    //清屏
    QToolButton *clearBtn = new QToolButton();
    clearBtn->setText("清除");
    ui->mainToolBar->addSeparator();
    ui->mainToolBar->addWidget(clearBtn);

    connect(clearBtn,QToolButton::clicked,[=](){
        localDrawBoard->pix->fill();
        update();
    });

}

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

main.cpp

#include "mainwindow.h"
#include 

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

    return a.exec();
}

你可能感兴趣的:(Qt,qt,ui,开发语言)