QT学习(4)简单绘图

绘制线段、矩形、椭圆。

在Widget类中作如下修改:

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
protected:
    void paintEvent(QPaintEvent *);
private:
    Ui::Widget *ui;
};

 paintEvent实现:

void Widget::paintEvent(QPaintEvent *)
{
//    QPainter p(this);
    QPainter p;
    p.begin(this);

//    p.drawPixmap(0, 0, width(), height(), QPixmap("../Image/test.jpg"));
//    p.drawPixmap(rect(), QPixmap("../Image/test.jpg"));
    QPen pen;
    pen.setWidth(5);
    pen.setColor(QColor(123,42,53));
    pen.setStyle(Qt::DashLine);
    p.setPen(pen);

    p.drawLine(50, 50, 150, 50);
    p.drawLine(50, 50, 50, 150);

    QBrush brush;
    brush.setColor(Qt::red);
    brush.setStyle(Qt::Dense6Pattern);
    p.setBrush(brush);

    p.drawRect(150, 150, 100, 50);

    p.drawEllipse(QPoint(150, 150), 50, 25);

    p.end();
}

效果:

QT学习(4)简单绘图_第1张图片

整理自B站网课,侵删歉。 

你可能感兴趣的:(QT笔记)