Qt绘图事件paintEvent
作者:互联网
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPainter>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void paintEvent(QPaintEvent *event) override
{
/* 实例化画家对象 this指定的是绘图设备 */
QPainter painter(this);
/* 设置画笔 */
QPen pen(QColor(255, 0, 0));
pen.setWidth(2);
pen.setStyle(Qt::DotLine); //设置画笔风格
painter.setPen(pen); //让画家使用这个笔
/* 设置画刷 */
QBrush brush(Qt::green);
brush.setStyle(Qt::Dense2Pattern); //设置画刷风格
painter.setBrush(brush); //让画家使用画刷
/* 画线 */
painter.drawLine(QPoint(0, 0), QPoint(100, 100));
/* 画圆 */
painter.drawEllipse(QPoint(100, 100), 50, 50);
/* 画矩形 */
painter.drawRect(QRect(150, 150, 50, 50));
/* 画文字 */
painter.drawText(QPoint(0, 250), "hello furong");
}
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
标签:Widget,Qt,QPoint,pen,绘图,paintEvent,100,painter 来源: https://www.cnblogs.com/zhangxuechao/p/16549369.html