代码下载链接:  http://pan.baidu.com/s/1hsc41Ek 密码: 5hdg

显示效果如下:

Qt5_简易画板_详细注释-LMLPHP

代码附有详细注释(代码如下)

 /***
* 先新建QMainWindow, 项目名称: DrawWidget 基类选择: QMainWindow,
* 类名默认, 然后在DrawWidget项目名上新建c++class文件, 选择基类: QWidget
*/
//先完成绘图区的实现
//如下为: drawwidget.h
#ifndef DRAWWIDGET_H
#define DRAWWIDGET_H #include <QWidget>
#include <QtGui>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QColor>
#include <QPixmap>
#include <QPoint>
#include <QPainter>
#include <QPalette> class DrawWidget : public QWidget
{
Q_OBJECT
public:
explicit DrawWidget(QWidget *parent = );
//鼠标事件重定义
void mousePressEvent (QMouseEvent *);
void mouseMoveEvent (QMouseEvent *);
//重画事件重定义
void paintEvent (QPaintEvent *);
//尺寸变化事件重定义
void resizeEvent (QResizeEvent *);
signals:
public slots:
void setStyle (int);
void setWidth (int);
void setColor (QColor);
void clear ();
private:
QPixmap *pix;
QPoint startPos; //点类
QPoint endPos;
int style;
int weight;
QColor color;
}; #endif // DRAWWIDGET_H
 //drawwidget.cpp
//DrawWidget构造函数完成对窗体参数及部分功能的初始化工作
#include "drawwidget.h"
#include <QtGui>
#include <QPen> DrawWidget::DrawWidget(QWidget *parent) : QWidget(parent)
{
setAutoFillBackground (true); //对窗体背景色的设置
setPalette (QPalette(Qt::white)); //背景色为白
pix = new QPixmap(size()); //此QPixmap对象用来准备随时接受绘制的内容
pix->fill (Qt::white); //填充背景色为白色
setMinimumSize (, ); //设置绘制区窗体的最小尺寸
} //接受主窗体传来的线型风格参数
void DrawWidget::setStyle (int s)
{
style = s;
} //setWidth()接受主窗体传来的线宽参数值
void DrawWidget::setWidth (int w)
{
weight = w;
} //接受主窗体传来的画笔颜色值
void DrawWidget::setColor (QColor c)
{
color = c;
} //重定义鼠标按下事件--按下鼠标时,记录当前鼠标位置值startPos
void DrawWidget::mousePressEvent (QMouseEvent *e)
{
startPos = e->pos ();
} //重定义鼠标移动事件--默认情况下,在鼠标按下的同时拖曳鼠标时被触发.
//mouseTracking事件,可以通过设置setMouseTracking(bool enable)为true,
//则无论是否有鼠标键按下,只要鼠标移动,就会触发mouseMoveEvent()
//在此函数中,完成向QPixmap对象中绘图的工作.
void DrawWidget::mouseMoveEvent (QMouseEvent *e)
{
QPainter *painter = new QPainter; //新建一个QPainter对象
QPen pen; //新建一个QPen对象
//设置画笔的线型,style表示当前选择的线型是Qt::PenStyle枚举数据中的第几个元素
pen.setStyle ((Qt::PenStyle)style);
pen.setWidth (weight); //设置画笔的线宽值
pen.setColor (color); //设置画笔的颜色
/***
* 以QPixmap对象为QPaintDevice参数绘制,构造一个QPainter对象,
* 就立即开始对绘画设备进行绘制,此构造QPainter对象是短期的
* 由于当一个QPainter对象的初始化失败时构造函数不能提供反馈信息,
* 所以在绘制 外部设备时 应使用begin()和end()(Ps:如打印机外部设备)
*/
painter->begin (pix);
painter->setPen (pen); //将QPen对象应用到绘制对象当中
//绘制从startPos到鼠标当前位置的直线
painter->drawLine (startPos, e->pos ());
painter->end (); //绘制成功返回true
startPos = e->pos (); //更新鼠标的当前位置,为下次绘制做准备
update (); //重绘绘制区窗体
} /***
* 重画函数paintEvent()完成绘制区窗体的更新工作,只需要调用drawPixmap()函数将用于接收图形绘制的
* 的QPixmap对象绘制在绘制区窗体控件上即可.
*/
void DrawWidget::paintEvent (QPaintEvent *)
{
QPainter painter(this);
painter.drawPixmap (QPoint(,), *pix);
} /***
* 调整绘制区大小函数resizeEvent():
* 当窗体大小改变是,实际能够绘制的区域仍然没有改变,因为绘图的大小没有改变
* 所以窗体尺寸变化时,应及时调整用于绘制的QPixmap对象的尺寸大小
*/
void DrawWidget::resizeEvent (QResizeEvent *event)
{
//判断改变后的窗体长或宽是否大于原窗体的长和宽;
//若大于则进行相应调整;
if (height () > pix->height () || width () > pix->width ())
{
QPixmap *newPix = new QPixmap(size()); //创建一个新的QPixmap对象
newPix->fill (Qt::white); //填充新QPixmap对象newPix的颜色为白色背景色
QPainter p(newPix);
p.drawPixmap (QPoint(, ), *pix); //在newPix中绘制原pix中内容
pix = newPix; //将newPix赋值给Pix作为新的绘制图形接收对象
}
//否则直接调用QWidget的resizeEvent()函数返回
QWidget::resizeEvent (event); //完成其余工作 } /***
* clear()函数完成绘制区的清除工作,只需要一个新的,干净的QPixmap对象代替pix,并调用update()重绘即可
*/
void DrawWidget::clear ()
{
QPixmap *clearPix = new QPixmap(size());
clearPix->fill (Qt::white);
pix = clearPix;
update ();
}
 //以上为能够响应鼠标事件进行绘图功能的窗体类实现
//主窗口的实现
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow>
#include <QToolButton>
#include <QLabel>
#include <QComboBox> //下拉列表框
#include <QSpinBox> //自选盒
#include "drawwidget.h" class MainWindow : public QMainWindow
{
Q_OBJECT public:
MainWindow(QWidget *parent = );
~MainWindow();
void createToolBar(); //创建工具栏
public slots:
void ShowStyle(); //进行选择线型风格的槽函数
void ShowColor(); //选择颜色的槽函数
private:
DrawWidget *drawWidget; //创建能够响应鼠标事件进行绘图功能的窗体类
QLabel *styleLabel; //风格
QComboBox *styleComboBox;
QLabel *widthLabel; //线宽
QSpinBox *widthSpinBox; //线宽自旋框
QToolButton *colorBtn; //颜色工具
QToolButton *clearBtn; //清除按钮
}; #endif // MAINWINDOW_H
 //mainwindow.cpp
#include "mainwindow.h"
#include <QToolBar>
#include <QColorDialog> MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
drawWidget = new DrawWidget; //新建一个DrawWidget对象--能够响应鼠标事件进行绘图功能的窗体类
setCentralWidget (drawWidget); //新建的DrawWidget对象作为主窗口的中央窗体
createToolBar (); //实现一个工具栏
setMinimumSize (, ); //设置主窗口的最小尺寸
ShowStyle (); //初始化线型,设置控件中的当前值作为初始值
drawWidget->setWidth (widthSpinBox->value ()); //初始化线宽
drawWidget->setColor (Qt::black); //初始化颜色
} //工具栏创建
void MainWindow::createToolBar ()
{
QToolBar *toolBar = addToolBar ("Tool"); //为主窗口新建一个工具栏对象
styleLabel = new QLabel(tr("线型风格: ")); //创建线性选择控件
styleComboBox = new QComboBox;
styleComboBox->addItem (tr("SolidLine"),
static_cast<int>(Qt::SolidLine));
styleComboBox->addItem (tr("DashLine"),
static_cast<int>(Qt::DashLine));
styleComboBox->addItem (tr("DotLine"),
static_cast<int>(Qt::DotLine));
styleComboBox->addItem (tr("DashDotLine"),
static_cast<int>(Qt::DashDotLine));
styleComboBox->addItem (tr("DashDotDotLine"),
static_cast<int>(Qt::DashDotDotLine));
connect (styleComboBox, SIGNAL(activated(int)), this, SLOT(ShowStyle())); //关联相应的槽函数
widthLabel = new QLabel(tr("线宽: ")); //创建线宽选择控件
widthSpinBox = new QSpinBox;
connect (widthSpinBox, SIGNAL(valueChanged(int)), drawWidget, SLOT(setWidth(int))); colorBtn = new QToolButton; //创建颜色选择控件
QPixmap pixmap(, ); //颜色选择按钮控件上的图像
pixmap.fill (Qt::black); //填充黑色
colorBtn->setIcon (QIcon(pixmap)); //设置按钮图像
connect (colorBtn, SIGNAL(clicked(bool)), this, SLOT(ShowColor())); clearBtn = new QToolButton(); //创建清除按钮
clearBtn->setText (tr("清除"));
connect (clearBtn, SIGNAL(clicked(bool)), drawWidget, SLOT(clear())); toolBar->addWidget (styleLabel);
toolBar->addWidget (styleComboBox);
toolBar->addWidget (widthLabel);
toolBar->addWidget (widthSpinBox);
toolBar->addWidget (colorBtn);
toolBar->addWidget (clearBtn);
} //ShowStyle(),通过调用DrawWidget类的setStyle()函数将当前线型选择控件中的线型参数传给绘制区;
void MainWindow::ShowStyle ()
{
drawWidget->setStyle (styleComboBox->itemData (styleComboBox->currentIndex (),
Qt::UserRole).toInt ());
} //ShowColor(),通过DrawWidget类的setColor()函数将用户在标准颜色对话框中选择的颜色值传给绘制区
void MainWindow::ShowColor ()
{
QColor color = QColorDialog::getColor (static_cast<int>(Qt::black)); //默认为黑(static_cast<int>转换成int节省内存
//使用标准颜色对话框QColorDialog获得一个颜色值
if (color.isValid ())
{
//先将新选择的颜色传给绘制区,用于改变画笔的颜色值
drawWidget->setColor (color);
//改变按钮图案
QPixmap p(, ); //设置图像大小
p.fill (color); //填充颜色
colorBtn->setIcon (QIcon(p)); //设置颜色按钮图案
}
} MainWindow::~MainWindow()
{
}
 //main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QFont> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFont font("ZYSong18030", );
a.setFont (font); MainWindow w;
w.show(); return a.exec();
}
05-11 14:07