QPainter QPen QBrush QPolygon

void painter::paintEvent(QPaintEvent *e)
{
    QPainter pt(this);
    QPen pen1(Qt::black);
    pen1.setWidth(2);

    QPen pen2(Qt::red);
    pen2.setWidth(5);

    QPoint p1;
    p1.setX(10);
    p1.setY(10);

    QPoint p2;
    p2.setX(200);
    p2.setY(500);

    pt.setPen(pen1);
    pt.drawLine(p1,p2);

    pt.setPen(pen2);
    pt.drawPoint(p1);
    pt.drawPoint(p2);

}
QPainter Basic

 

void painter::paintEvent(QPaintEvent *e)
{
    QPainter pt(this);
    QRect rect(10,10,100,100);
    QPen pen(Qt::red);
    pen.setWidth(6);

    QBrush brush(Qt::blue, Qt::CrossPattern);

    pt.fillRect(rect,brush);
    pt.drawRect(rect);
    pt.setPen(pen);
    pt.drawEllipse(rect);

}
Draw rectangle & elipse

 

void painter::paintEvent(QPaintEvent *e)
{
    QPainter pt(this);

    //make our polygon
    QPolygon poly;
    poly << QPoint(10, 10);
    poly << QPoint(10, 100);
    poly << QPoint(100, 10);
    poly << QPoint(100, 100);

    //make a pen;
    QPen pen1;
    pen1.setWidth(5);
    pen1.setColor(Qt::blue);
    //pen1.setJoinStyle(Qt::RoundJoin); // round cornor
    pen1.setJoinStyle(Qt::MiterJoin);  //shape cornor
    pen1.setStyle((Qt::DotLine));  // dot stroke
    pt.setPen(pen1);

    //make a brush
    QBrush fillBrush;
    fillBrush.setColor(Qt::red);
    fillBrush.setStyle(Qt::SolidPattern);

    //fill the polygon
    QPainterPath path;
    path.addPolygon(poly);

    pt.fillPath(path, fillBrush);
    //draw polygon
    pt.drawPolygon(poly);

}
draw Polygon

void painter::paintEvent(QPaintEvent *e)
{
    QPainter pt(this);
    QPen pen(Qt::black);
    pen.setWidth(15);

    QPoint startpointer(20, 30);
    QPoint endpointer(200, 30);
    //draw first line
    pen.setCapStyle(Qt::FlatCap);
    pt.setPen(pen);
    pt.drawLine(startpointer, endpointer);
    //draw second line
    startpointer.setY(50);
    endpointer.setY(50);
    pen.setCapStyle(Qt::SquareCap);
    pt.setPen(pen);
    pt.drawLine(startpointer, endpointer);
    //draw third line
    startpointer.setY(70);
    endpointer.setY(70);
    pen.setCapStyle(Qt::RoundCap);
    pt.setPen(pen);
    pt.drawLine(startpointer, endpointer);
}
draw stylized line

void paintWidget::paintEvent(QPaintEvent *e)
{
    QPainter pt(this);

    QLinearGradient grad1(25, 100, 150,175);
    QRadialGradient grad2(QPoint(100, 100), 100);
    QConicalGradient grad3(QPoint(100, 100), 10);
    
    grad1.setColorAt(0.0, Qt::white);
    grad1.setColorAt(0.2, Qt::green);
    grad1.setColorAt(1.0, Qt::black);

    QRect rec(10, 10, 200, 200);
    pt.fillRect(rec, grad1);
}
Gradiant

void paintWidget::paintEvent(QPaintEvent *e)
{
    QPainter pt(this);

    QRect rec(100, 120, 250, 25);
    pt.drawRect(rec);
    pt.drawText(rec, Qt::AlignLeft, "Left");
    pt.drawText(rec, Qt::AlignCenter, "Center");
    pt.drawText(rec, Qt::AlignRight | Qt::AlignVCenter, "Right");

    QRect rec2(100, 150, 25, 25);
    pt.drawRect(rec2);
    pt.drawText(rec2, "This will be clipped.");

    QTextDocument doc;
    QRect rec3(0, 0, 250, 250);
    pt.translate(20, 20);
    doc.setTextWidth(rec3.width());
    doc.setHtml("<b><u>Hello World!</b></u>");
    doc.drawContents(&pt, rec3);
}
draw QTextDocument

void paintWidget::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);

    QPen pen1(Qt::black);
    pen1.setWidth(6);

    QPen pen2(Qt::red);
    pen2.setWidth(6);

    QPen pen3(Qt::green);
    pen3.setWidth(6);

    QPen pen4(Qt::blue);
    pen4.setWidth(6);

    QRect rec(100, 100, 100, 100);
    painter.setPen(pen1);
    painter.drawRect(rec);

    painter.translate(150, -60);
    painter.rotate(45);
    painter.setPen(pen2);
    painter.drawRect(rec);

    painter.setPen(pen3);
    painter.scale(0.5, 0.5);
    painter.drawRect(rec);

    painter.resetTransform();
    painter.shear(0.2, 0.2);
    painter.setPen(pen4);
    painter.drawRect(rec);
}
Transformation

scene = new QGraphicsScene(this);
    view = new QGraphicsView();
    view->setScene(scene);

    QHBoxLayout *lyt = new QHBoxLayout;
    lyt->addWidget(view);
    setLayout(lyt);

    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);
    QPen blackPen(Qt::black);
    blackPen.setWidth(5);

    ellipse = scene->addEllipse(50, 50, 100, 100, blackPen,redBrush);
    rectangle = scene->addRect(50, 50, 50, 50, blackPen, blueBrush);
    rectangle->setFlag(QGraphicsItem::ItemIsMovable);
QGraphicScene

 

posted @ 2018-06-12 23:35  Bear_Guo  阅读(331)  评论(0编辑  收藏  举报