基本图形
void PaintArea::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setPen(pen);
p.setBrush(brush);
QRect rect(50,100,300,200);
static const QPoint points[4] = {
QPoint(150, 100),
QPoint(300, 150),
QPoint(350, 250),
QPoint(100, 300)
};
int startAngle = 30 * 16;
int spanAngle = 120 * 16;
QPainterPath path;
path.addRect(150,150,100,100);
path.moveTo(100,100);
path.cubicTo(300,100,200,200,300,300);
path.cubicTo(100,300,200,200,100,100);
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);
break;
case Polyline://多边线
p.drawPolyline(points,4);
break;
case Points:
p.drawPoints(points,4);
break;
case Arc:
p.drawArc(rect,startAngle,spanAngle);//startAngle,spanAngle都是1/16度为单位
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(":/images/butterfly.png"));
break;
default:
break;
}
}
设置渐变
MainWidget::slotBrush(int value)
{
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, Qt::green);
linearGradient.setColorAt(1.0, Qt::black);
linearGradient.setSpread(mainWidget->spread);//渐变的铺展效果,
area->setBrush(linearGradient);
} else if (style == Qt::RadialGradientPattern) {
QRadialGradient radialGradient(200, 200, 150, 150, 100);
radialGradient.setColorAt(0.0, Qt::white);
radialGradient.setColorAt(0.2, Qt::green);
radialGradient.setColorAt(1.0, Qt::black);
radialGradient.setSpread(mainWidget->spread);
area->setBrush(radialGradient);
} else if (style == Qt::ConicalGradientPattern) {
QConicalGradient conicalGradient(200, 200, 30);
conicalGradient.setColorAt(0.0, Qt::white);
conicalGradient.setColorAt(0.2, Qt::green);
conicalGradient.setColorAt(1.0, Qt::black);
area->setBrush(conicalGradient);//conicalGradientt先默认构建成QBrush,再作为QPainter函数参数
} else if (style == Qt::TexturePattern) {
area->setBrush(QBrush(QPixmap(":/images/cheese.jpg")));
} else {
area->setBrush(QBrush(Qt::green, style));
}
}
设置画笔
MainWidget::slotPenJoin(int value)
{
int width = widthSpinBox->value();
QColor color = colorFrame->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(value,
Qt::UserRole).toInt());
area->setPen(QPen(color, width, style, cap, join));
}
利用QPainterPath画图
QPainterPath path;
path.addRect(150,150,100,100);
path.moveTo(100,100);
path.cubicTo(300,100,200,200,300,300);
path.cubicTo(100,300,200,200,100,100);
p.drawPath(path);
QPainter::addRect()可以用如下方式实现
path.moveTo(0,0);
path.lineTo(200,0);
path.lineTo(200,100);
path.lineTo(0,100);
path.lineTo(0,0);