本节对应的视频讲解:B_站_视_频
https://www.bilibili.com/video/BV1qd4y1s7xk
布局完成后,就可以修改控件的名称,以及添加初始化数据
1. 变量命名
先修改各控件显示的的名称,做到 “见名知义”,方便写代码
修改完控件名称后的效果如下:
2. 初始化数据
为各个控件添加 初始化数据
2.1 形状
为 cboShape 控件,添加形状
首先,在 paintwidget.h 中定义一个枚举类型,表示形状的类型,如下:
enum Shape {
_Point, //
_Line, //
_Rect, //
_RoundedRect, //
_Ellipse, //
_Polyline, //
_Polygon, //
_Arc, //
_Pie, //
_Chord, //
_Path, //
_Text, //
_Pixmap
};
然后,在 widget.cpp 的构造中,添加如下初始化代码:
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
// 1. 形状
ui->cboShape->addItem("Point", static_cast<int>(_Point));
ui->cboShape->addItem("Line", static_cast<int>(_Line));
ui->cboShape->addItem("Rectangle", static_cast<int>(_Rect));
ui->cboShape->addItem("Rounded Rectangle", static_cast<int>(_RoundedRect));
ui->cboShape->addItem("Ellipse", static_cast<int>(_Ellipse));
ui->cboShape->addItem("Arc", static_cast<int>(_Arc));
ui->cboShape->addItem("Pie", static_cast<int>(_Pie));
ui->cboShape->addItem("Chord", static_cast<int>(_Chord));
ui->cboShape->addItem("Polyline", static_cast<int>(_Polyline));
ui->cboShape->addItem("Polygon", static_cast<int>(_Polygon));
ui->cboShape->addItem("Path", static_cast<int>(_Path));
ui->cboShape->addItem("Text", static_cast<int>(_Text));
ui->cboShape->addItem("Pixmap", static_cast<int>(_Pixmap));
}
2.2 画笔
初始化画笔的属性
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
// 2. 线宽
ui->sbPenWidth->setRange(1, 10);
// 3. 线型
// 3.1 线型-实线、虚线、点画线等
ui->cboPenStyle->addItem("Solid", static_cast<int>(Qt::SolidLine));
ui->cboPenStyle->addItem("Dash", static_cast<int>(Qt::DashLine));
ui->cboPenStyle->addItem("Dot", static_cast<int>(Qt::DotLine));
ui->cboPenStyle->addItem("Dash Dot", static_cast<int>(Qt::DashDotLine));
ui->cboPenStyle->addItem("Dash Dot Dot", static_cast<int>(Qt::DashDotDotLine));
ui->cboPenStyle->addItem("None", static_cast<int>(Qt::NoPen));
// 3.2 线型-线的端点的样式
ui->cboPenCap->addItem("Flat", static_cast<int>(Qt::FlatCap));
ui->cboPenCap->addItem("Square", static_cast<int>(Qt::SquareCap));
ui->cboPenCap->addItem("Round", static_cast<int>(Qt::RoundCap));
// 3.3 连接-两根线的连接处的样式
ui->cboPenJoin->addItem("Miter", static_cast<int>(Qt::MiterJoin));
ui->cboPenJoin->addItem("Bevel", static_cast<int>(Qt::BevelJoin));
ui->cboPenJoin->addItem("Round", static_cast<int>(Qt::RoundJoin));
}
2.3 画刷
初始化画刷的属性
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
// 4. 画刷
ui->cboBrushStyle->addItem(tr("Linear Gradient"), static_cast<int>(Qt::LinearGradientPattern));
ui->cboBrushStyle->addItem(tr("Radial Gradient"), static_cast<int>(Qt::RadialGradientPattern));
ui->cboBrushStyle->addItem(tr("Conical Gradient"), static_cast<int>(Qt::ConicalGradientPattern));
ui->cboBrushStyle->addItem(tr("Texture"), static_cast<int>(Qt::TexturePattern));
ui->cboBrushStyle->addItem(tr("Solid"), static_cast<int>(Qt::SolidPattern));
ui->cboBrushStyle->addItem(tr("Horizontal"), static_cast<int>(Qt::HorPattern));
ui->cboBrushStyle->addItem(tr("Vertical"), static_cast<int>(Qt::VerPattern));
ui->cboBrushStyle->addItem(tr("Cross"), static_cast<int>(Qt::CrossPattern));
ui->cboBrushStyle->addItem(tr("Backward Diagonal"), static_cast<int>(Qt::BDiagPattern));
ui->cboBrushStyle->addItem(tr("Forward Diagonal"), static_cast<int>(Qt::FDiagPattern));
ui->cboBrushStyle->addItem(tr("Diagonal Cross"), static_cast<int>(Qt::DiagCrossPattern));
ui->cboBrushStyle->addItem(tr("Dense 1"), static_cast<int>(Qt::Dense1Pattern));
ui->cboBrushStyle->addItem(tr("Dense 2"), static_cast<int>(Qt::Dense2Pattern));
ui->cboBrushStyle->addItem(tr("Dense 3"), static_cast<int>(Qt::Dense3Pattern));
ui->cboBrushStyle->addItem(tr("Dense 4"), static_cast<int>(Qt::Dense4Pattern));
ui->cboBrushStyle->addItem(tr("Dense 5"), static_cast<int>(Qt::Dense5Pattern));
ui->cboBrushStyle->addItem(tr("Dense 6"), static_cast<int>(Qt::Dense6Pattern));
ui->cboBrushStyle->addItem(tr("Dense 7"), static_cast<int>(Qt::Dense7Pattern));
ui->cboBrushStyle->addItem(tr("None"), static_cast<int>(Qt::NoBrush));
}
经过以上的初始化操作,效果如下:
3. 添加资源文件
由于后边有绘制图片,以及将图片作为画刷的填充,因此这里先把图片作为资源添加到工程中来
首先,将包含两种图片文件 brick.png 和 qt-logo.png 的 images 文件夹,拷贝到项目目录下
然后,在项目名称右键,选择 “添加新文件…”,创建一个 painter.qrc 的资源文件:
添加之后的效果,如下:
添加完毕之后,就可以直接在代码中引用这两个图片了!