类QPainter的常用语句:
void PainterTest::paintEvent(QPaintEvent *)
{
QPainter painter(this);
/*QPen pen; //画笔
pen.setColor(QColor(255,0,0));
QBrush brush(QColor(0,255,0,125)); //画刷
*/
QPen pen(Qt::DotLine);
QBrush brush(Qt::blue);
brush.setStyle(Qt::HorPattern);
painter.setPen(pen); //添加画笔
painter.setBrush(brush); //添加画刷
/*画弧形*/
QRectF rectangle(10.0, 20.0, 80.0, 60.0); //矩形
int startAngle = 30 * 16; //起始角度
int spanAngle = 120 * 16; //跨越度数
/*设置渐变*/
QLinearGradient linearGradient(100,150,300,150);
//从点(100,150)开始到点(300,150)结束,确定一条直线
linearGradient.setColorAt(0,Qt::red);
linearGradient.setColorAt(0.2,Qt::black);
linearGradient.setColorAt(0.4,Qt::yellow);
linearGradient.setColorAt(0.6,Qt::white);
linearGradient.setColorAt(0.8,Qt::green);
linearGradient.setColorAt(1,Qt::blue);//将直线开始点设为0,终点设为1,然后分段设置颜色
painter.setBrush(linearGradient);
/*圆锥渐变*/
// QConicalGradient conicalGradient(50,50,0);//圆心为(50,50),开始角度为0
// conicalGradient.setColorAt(0,Qt::green);
//conicalGradient.setColorAt(1,Qt::white); //从圆心的0度角开始逆时针填充
//painter.setBrush(conicalGradient);
/*矩形框中画字体*/
QRectF ff(10,10,30,40); //设置一个矩形
painter.drawRect(ff); //为了更直观地看到字体的位置,我们绘制出这个矩形
painter.setPen(QColor(Qt::blue));//设置画笔颜色为红色
painter.drawText(ff,Qt::AlignHCenter,"World");
/*设置字体*/
QFont font("Arial",20,QFont::Bold,true);
//设置字体的类型,大小,加粗,斜体
font.setUnderline(true);
//设置下划线
font.setOverline(true);
//设置上划线
font.setCapitalization(QFont::SmallCaps);
//设置大小写
font.setLetterSpacing(QFont::AbsoluteSpacing,5);
//设置间距
painter.setFont(font);
painter.drawLine(0,0,100,100);//画直线
painter.drawArc(rectangle, startAngle, spanAngle);//画弧线
painter.drawRect(100,100,200,100);//又画了个矩形
painter.drawEllipse(100,0,200,200);//画圆
}
QpinterPath类常用函数:
void PainterTest::paintEvent(QPaintEvent *)
{
QPainterPath path;
path.lineTo(100,100);
path.lineTo(200,100);
path.addEllipse(100,100,50,50);
path.lineTo(200,200);
QPainter painter(this);
painter.setPen(Qt::green);
painter.setBrush(Qt::yellow);
painter.drawPath(path);
QPainterPath path2;
path2.addPath(path);
path2.translate(100,0);
path2.moveTo(100,100);//可见moveTo函数可以改变当前点的位置
path2.lineTo(300,300);
painter.drawPath(path2);
}
void PainterTest::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pix;
pix.load("images/4.ico");
painter.drawPixmap(0,0,100,100,pix);
painter.translate(50,50); //让图片的中心作为旋转的中心
painter.rotate(90); //顺时针旋转90度
qreal width = pix.width(); //获得以前图片的宽和高
qreal height = pix.height();
pix = pix.scaled(width*2,height*2,Qt::KeepAspectRatio);
//将图片的宽和高都扩大两倍,并且在给定的矩形内保持宽高的比值
painter.drawPixmap(100,100,pix);
}