QT二维图形的绘制(类的介绍与绘图实例)
2015-11-11 21:04
881 查看
1.告诉绘制引擎一些东西
QPainter::Antialiasing 在可能的情况下,反锯齿QPainter::TextAntialiasing 在可能的情况下,文字反锯齿
QPainter::SmoothPixmapTransform 采用平滑的Pixmap变换算法
2.绘制函数
3.风格
1)画刷风格2)画笔风格
3)画笔连接点风格
4)顶端风格
4.绘图前的准备
1 如果要在绘图设备(一般为窗口部件)上绘图,只需创建一个QPainter,再将指针传到该设备中。例如:
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
}
2 常用的初始化:画笔、画刷、字体
画笔:用来画线和边缘。它包含颜色、宽度、线性、拐点风格以及连线风格。
画刷:用来填充几何图形的图案。它一般由颜色和风格组成,但同时也可以是纹理(一个不断重复的图像)或者是一个渐变。
字体:用来绘制文字。字体有很多属性,包括字体族和磅值大小。
5.具体的实例
通过建立一个画图面板来了解Qpainter是怎样绘制图形的,其中包含两个类,画图的区域PainterArea类和主窗口mainWindow类(1)实现PainterArea类
//paintarea.h
#ifndef PAINTAREA_H
#define PAINTAREA_H
#include <QWidget>
#include<QPen>
#include<QPaintEvent>
class PaintArea : public QWidget
{
Q_OBJECT
public:
enum Shape{Line,Rectangle,RoundRect,Ellipse,Polygon,Polyline,Points,Arc,Path,Text,
Pixmap};
PaintArea(QWidget * parent=0);
void setShape(Shape); //设置形状
void setPen(QPen); //设置画笔
void setBrush(QBrush); //设置画刷
void setFillRule(Qt::FillRule);//设置填充模式
void paintEvent(QPaintEvent *);//重画事件
private:
Shape shape;
QPen pen;
QBrush brush;
Qt::FillRule fillRule;
};
#endif // PAINTAREA_H
//paintarea.cpp
#include "paintarea.h"
#include <QPainter>
PaintArea::PaintArea(QWidget *parent)
:QWidget(parent)
{
setPalette(QPalette(Qt::white)); //设置背景颜色
setAutoFillBackground(true);//设置自动填充背景色
setMinimumSize(400,400);//设置窗口最下大小
}
void PaintArea::setShape(Shape s) //update()更新窗口部件
{
shape=s;
update();
}
void PaintArea::setPen(QPen p)
{
pen=p;
update();
}
void PaintArea::setBrush(QBrush b)
{
brush=b;
update();
}
void PaintArea::setFillRule(Qt::FillRule rule)
{
fillRule=rule;
update();
}
//重画事件
void PaintArea::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setPen(pen);
p.setBrush(brush);
QRect rect(100,100,250,200);//构造一个矩形
static const QPoint points[4]={
QPoint(100,100),
QPoint(200,150),
QPoint(300,250),
QPoint(150,300)
};//确定Points的四个点的坐标
int startAngle=60*16;
int spanAngle=180*16; //为绘制曲线设置参数变量
QPainterPath path; //QPainterPath为Qpainter类提供了一个存储容器,里面包含了画的内容和画的顺序,
path.moveTo(50,150); //当前位置移动到坐标50,150
path.lineTo(350,150); //当前位置开始画直线,终点位置坐标350,150
path.lineTo(100,325);
path.lineTo(200,50);
path.lineTo(300,325);
path.lineTo(50,150);
path.setFillRule(fillRule); //设置填充模式
switch(shape)
{
case Line: //直线
p.drawLine(rect.topLeft(),rect.bottomRight()); //绘制直线。起点为矩形左上点,终点为矩形右下点
break;
case Rectangle: //长方形
p.drawRect(rect);
break;
case RoundRect: //圆角方形
p.drawRoundRect(rect);
break;
case Ellipse: //椭圆形
p.drawEllipse(rect);
break;
case Polygon: //多边形
p.drawPolygon(points,4); //绘制4个顶点多边形
break;
case Polyline: //多边线
p.drawPolyline(points,4);
break;
case Points: //点
p.drawPoints(points,4);
break;
case Arc: //弧
p.drawArc(rect,startAngle,spanAngle); //后面两个参数分别为 起始角与跨度角
break;
case Path: //绘制之前已经画好的路径
p.drawPath(path);
break;
case Text: //文字
p.drawText(rect,Qt::AlignCenter,tr("Hello Qt!"));
break;
case Pixmap: //图片
p.drawPixmap(150,150,QPixmap("1.png"));//绘制一个图像
break;
default:
break;
}
}
//前面的椭圆形、直线、长方形、圆角矩形,弧 都是利用矩形为骨架构成的,具体如何构成参照介绍中的绘制函数中的图。
(2)实现主窗口类MainWidget
//mainwindow.h头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include<paintarea.h>
#include <QLabel>
#include <QComboBox>
#include <QSpinBox>
#include<QPushButton>
#include <QGridLayout>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
PaintArea *paintArea;
QLabel *shapelabel;//形状
QComboBox *shapeComboBox;
QLabel *penWidthLable;//画笔宽度
QSpinBox *penWidthSpinBox;
QLabel *penColorLabel;//画笔颜色
QFrame *penColorFrame;
QPushButton *penColorbtn;//画笔颜色按钮
QLabel *penStyleLabel;
QComboBox *penStyleComboBox;//画笔风格
QLabel *penCapLabel;
QComboBox *penCapComboBox;
QLabel *penJoinLabel;
QComboBox *penJoinComboBox;
QLabel *fillLabel;
QComboBox *fillComboBox;
QLabel *brushStyleLabel;
QComboBox *brushStyleComboBox;
QLabel *brushColorlabel;
QFrame *brushColorFrame;
QPushButton *brushColorbtn;
QGridLayout *settingLayout;
protected slots:
void ShowShape(int);
void ShowPenWidth(int);
void ShowPenColor();
void ShowPenStyle(int);
void ShowPenCap(int);
void ShowPenJoin(int);
void ShowFill();
void ShowBrushColor();
void ShowBrush(int);
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include"mainwindow.h"
#include "paintarea.h"
#include<QColorDialog>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent)
{
paintArea=new PaintArea;
shapelabel=new QLabel("形状:"); //形状选择下拉列表框
shapeComboBox=new QComboBox;
shapeComboBox->addItem(tr("Line"),PaintArea::Line);
shapeComboBox->addItem(tr("RoundRect"),PaintArea::RoundRect);
shapeComboBox->addItem(tr("Ellipse"),PaintArea::Ellipse);
shapeComboBox->addItem(tr("Polygon"),PaintArea::Polygon);
shapeComboBox->addItem(tr("Polyline"),PaintArea::Polyline);
shapeComboBox->addItem(tr("Points"),PaintArea::Points);
shapeComboBox->addItem(tr("Arc"),PaintArea::Arc);
shapeComboBox->addItem(tr("Rectangle"),PaintArea::Rectangle);
shapeComboBox->addItem(tr("Path"),PaintArea::Path);
shapeComboBox->addItem(tr("Text"),PaintArea::Text);
shapeComboBox->addItem(tr("Pixmap"),PaintArea::Pixmap);
connect(shapeComboBox,SIGNAL(activated(int)),this,SLOT(ShowShape(int)));
penColorLabel=new QLabel("画笔颜色:"); //画笔颜色选择控件
penColorFrame=new QFrame;
penColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);//Panel - QFrame画一个平板使内容看起来凸起或者凹陷, //QFrame::Sunken - 框架和内容看起来凹陷
penColorFrame->setAutoFillBackground(true);
penColorFrame->setPalette(QPalette(Qt::blue));//设置默认颜色为蓝色
penColorbtn=new QPushButton;
connect(penColorbtn,SIGNAL(clicked()),this,SLOT(ShowPenColor()));
penWidthLable=new QLabel("画笔宽度:");//画笔宽度选择控件
penWidthSpinBox=new QSpinBox;
penWidthSpinBox->setRange(0,20);//设置字宽滑块的取值范围
connect(penWidthSpinBox,SIGNAL(valueChanged(int)),this,SLOT(ShowPenWidth(int)));
penStyleLabel=new QLabel; //画笔风格选择下拉列表框
penStyleComboBox=new QComboBox;
penStyleComboBox->addItem(tr("SolidLine"),Qt::SolidLine);
penStyleComboBox->addItem(tr("DashLine"),Qt::DashLine);
penStyleComboBox->addItem(tr("DotLine"),Qt::DotLine);
penStyleComboBox->addItem(tr("DashDotLine"),Qt::DashDotLine);
penStyleComboBox->addItem(tr("DashDotDotLine"),Qt::DashDotDotLine);
penStyleComboBox->addItem(tr("CustomDashLine"),Qt::CustomDashLine);//这段如果报错,可以把addItem后面的参数置换为int类的数字
connect(penStyleComboBox,SIGNAL(activated(int)),this,SLOT(ShowPenStyle(int)));
penCapLabel=new QLabel; //画顶端风格选择下拉列表框
penCapComboBox=new QComboBox;
penCapComboBox->addItem(tr("SquareCap"),Qt::SquareCap);
penCapComboBox->addItem(tr("FlatCap"),Qt::FlatCap);
penCapComboBox->addItem(tr("RoundCap"),Qt::RoundCap);
connect(penCapComboBox,SIGNAL(activated(int)),this,SLOT(ShowPenCap(int)));
penJoinLabel=new QLabel;//画笔连接点风格选择下拉列表框
penJoinComboBox=new QComboBox;
penJoinComboBox->addItem(tr("BevelJoin"),Qt::BevelJoin);
penJoinComboBox->addItem(tr("MiterJoin"),Qt::MiterJoin);
penJoinComboBox->addItem(tr("RoundJoin"),Qt::RoundJoin);
fillLabel=new QLabel;//填充模式选择下拉列表框
fillComboBox=new QComboBox;
fillComboBox->addItem(tr("Odd Even"),Qt::OddEvenFill);
fillComboBox->addItem(tr("Winding"),Qt::WindingFill);
connect(fillComboBox,SIGNAL(activated(int)),this,SLOT(ShowFill()));
brushColorlabel=new QLabel;//画刷风格选择下拉列表框
brushColorFrame=new QFrame;
brushColorbtn=new QPushButton;
brushColorFrame->setFrameStyle(QFrame::Panel|QFrame::Sunken);
brushColorFrame->setAutoFillBackground(true);
brushColorFrame->setPalette(QPalette(Qt::green));
connect(brushColorbtn,SIGNAL(clicked()),this,SLOT(ShowBrushColor()));
brushStyleLabel=new QLabel; //画刷风格选择下拉列表框
brushStyleComboBox=new QComboBox;
brushStyleComboBox->addItem(tr("SolidPattern"),1);
brushStyleComboBox->addItem(tr("Dense1Pattern"),2);
brushStyleComboBox->addItem(tr("Dense2Pattern"),3);
brushStyleComboBox->addItem(tr("Dense3Pattern"),4);
brushStyleComboBox->addItem(tr("Dense4Pattern"),5);
brushStyleComboBox->addItem(tr("Dense5Pattern"),6);
brushStyleComboBox->addItem(tr("Dense6Pattern"),7);
brushStyleComboBox->addItem(tr("Dense7Pattern"),8);
brushStyleComboBox->addItem(tr("HorPattern"),9);
brushStyleComboBox->addItem(tr("VerPattern"),10);
brushStyleComboBox->addItem(tr("CrossPattern"),11);
brushStyleComboBox->addItem(tr("BDiagPattern"),12);
brushStyleComboBox->addItem(tr("FDiagPattern"),13);
brushStyleComboBox->addItem(tr("DiagCrossPattern"),14);
brushStyleComboBox->addItem(tr("LinearGradientPattern"),15);
brushStyleComboBox->addItem(tr("ConicalGradientPattern"),16);
brushStyleComboBox->addItem(tr("RadialGradientPattern"),17);
brushStyleComboBox->addItem(tr("TexturePattern"),24);
connect(brushStyleComboBox,SIGNAL(activated(int)),this,SLOT(ShowBrush(int)));
settingLayout=new QGridLayout;//画板布局
settingLayout->addWidget(shapelabel,0,0);
settingLayout->addWidget(shapeComboBox,0,1);
settingLayout->addWidget(penColorLabel,0,2);
settingLayout->addWidget(penColorFrame,0,3);
settingLayout->addWidget(penColorbtn,0,4);
settingLayout->addWidget(penWidthLable,1,0);
settingLayout->addWidget(penWidthSpinBox,1,1);
settingLayout->addWidget(penStyleLabel,1,2);
settingLayout->addWidget(penStyleComboBox,1,3);
settingLayout->addWidget(penCapLabel,2,0);
settingLayout->addWidget(penCapComboBox,2,1);
settingLayout->addWidget(penJoinLabel,2,2);
settingLayout->addWidget(penJoinComboBox,2,3);
settingLayout->addWidget(fillLabel,3,0);
settingLayout->addWidget(fillComboBox,3,1);
settingLayout->addWidget(brushColorlabel,3,2);
settingLayout->addWidget(brushColorFrame,3,3);
settingLayout->addWidget(brushColorbtn,3,4);
settingLayout->addWidget(brushStyleLabel,4,0);
settingLayout->addWidget(brushStyleComboBox,4,1);
QVBoxLayout *mainLayout=new QVBoxLayout(this);
mainLayout->addLayout(settingLayout);
mainLayout->addWidget(paintArea);
ShowShape(shapeComboBox->currentIndex());
}
//以下就是各种槽函数了
void MainWindow::ShowShape(int value)
{
PaintArea::Shape shape=PaintArea::Shape(shapeComboBox->itemData(value,Qt::UserRole).toInt());
paintArea->setShape(shape);
}
void MainWindow::ShowPenWidth(int value)//画笔宽度
{
QColor color=penColorFrame->palette().color(QPalette::Window);
Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(penCapComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowPenColor()//画笔颜色
{
QColor color=QColorDialog::getColor(Qt::blue);
penColorFrame->setPalette(QPalette(color));
int value=penWidthSpinBox->value();
Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(penCapComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowPenStyle(int styleValue)//画笔风格
{
QColor color=penColorFrame->palette().color(QPalette::Window);
int value=penWidthSpinBox->value();
Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(styleValue,Qt::UserRole).toInt());
Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(penCapComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowPenCap(int capValue)//顶端风格
{
QColor color=penColorFrame->palette().color(QPalette::Window);
int value=penWidthSpinBox->value();
Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(capValue,Qt::UserRole).toInt());
Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(penJoinComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowPenJoin(int joinValue)//画笔连接点风格
{
QColor color=penColorFrame->palette().color(QPalette::Window);
int value=penWidthSpinBox->value();
Qt::PenStyle style=Qt::PenStyle(penStyleComboBox->itemData(penStyleComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenCapStyle cap=Qt::PenCapStyle(penCapComboBox->itemData(penCapComboBox->currentIndex(),Qt::UserRole).toInt());
Qt::PenJoinStyle join=Qt::PenJoinStyle(penJoinComboBox->itemData(joinValue,Qt::UserRole).toInt());
paintArea->setPen(QPen(color,value,style,cap,join));
}
void MainWindow::ShowFill()//填充模式
{
Qt::FillRule fill=Qt::FillRule(fillComboBox->itemData(fillComboBox->currentIndex(),Qt::UserRole).toInt());
paintArea->setFillRule(fill);
}
void MainWindow::ShowBrushColor()//画刷颜色
{
QColor color=QColorDialog::getColor(Qt::blue);
brushColorFrame->setPalette(QPalette(color));
ShowBrush(brushStyleComboBox->currentIndex());
}
void MainWindow::ShowBrush(int value)//画刷风格
{
QColor color=brushColorFrame->palette().color(QPalette::Window);//画刷当前的颜色
Qt::BrushStyle style=Qt::BrushStyle(brushStyleComboBox->itemData(value,Qt::UserRole).toInt());//获取用户选择的画刷风格
if(style==Qt::LinearGradientPattern)//线性渐变
{
QLinearGradient linearGradient(0,0,400,400);//线性渐变的起止点位置
linearGradient.setColorAt(0.0,Qt::white);//渐变比和渐变颜色
linearGradient.setColorAt(0.2,color);
linearGradient.setColorAt(1.0,Qt::black);
paintArea->setBrush(linearGradient);
}
else if(style==Qt::RadialGradientPattern)//圆形渐变
{
QRadialGradient radialGradient(200,200,150,150,100);//中心点,半径,焦点
radialGradient.setColorAt(0.0,Qt::white);
radialGradient.setColorAt(0.2,color);
radialGradient.setColorAt(1.0,Qt::black);
paintArea->setBrush(radialGradient);
}
else if(style==Qt::ConicalGradientPattern)//锥形渐变
{
QConicalGradient conicalGradient(200,200,30);
conicalGradient.setColorAt(0.0,Qt::white);
conicalGradient.setColorAt(0.2,color);
conicalGradient.setColorAt(1.0,Qt::black);
paintArea->setBrush(conicalGradient);
}
else if(style==Qt::TexturePattern)
{
paintArea->setBrush(QBrush(QPixmap("2.png")));
}
else
{
paintArea->setBrush(QBrush(color,style));
}
}
MainWindow::~MainWindow()
{
}
相关文章推荐
- QT学习 第一章:基本对话框
- 使用Shiboken为C++和Qt库创建Python绑定
- VB实现鼠标绘图实例代码
- Windows窗体的.Net框架绘图技术实现方法
- Qt定时器和随机数详解
- php绘图中显示不出图片的原因及解决
- Qt实现图片移动实例(图文教程)
- php绘图之生成饼状图的方法
- php绘图之加载外部图片的方法
- php绘图之在图片上写中文和英文的方法
- Qt for Android开发实例教程
- php绘制一条直线的方法
- 使用Java的Graphics类进行绘图的方法详解
- Java中的Graphics2D类基本使用教程
- python海龟绘图实例教程
- 浅析PHP绘图技术
- python绘图方法实例入门
- QModelIndex/Role/Model介紹<二>
- 基于PyQt5的快速开发框架QFramer