赞
踩
绘制文本、直线、矩形、椭圆、多边形小例子
void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QRect rectWnd = this->rect();
// 绘制文字
QPainter painter(this);
// 设置画笔颜色
painter.setPen(QPen(Qt::black, 4, Qt::DashDotLine, Qt::RoundCap));
//设置字体
QFont font;
font.setFamily("楷体");
font.setPointSize(50); // 字体大小
font.setItalic(true); // 斜体
font.setBold(true); // 粗体
// 设置反走样
painter.setRenderHint(QPainter::Antialiasing);
// 设置字体
painter.setFont(font);
// 绘制文本
painter.drawText(0, 0, rectWnd.width() / 4, rectWnd.height() / 4, Qt::AlignCenter, "我们");
// 绘制直线
painter.drawLine(rectWnd.width() / 4, rectWnd.height() / 4, rectWnd.width() / 2, rectWnd.height() / 2);
// 绘制矩形
painter.setBrush(QBrush(QColor(0, 255, 0)));
painter.drawRect(10, rectWnd.width() / 4, rectWnd.width() / 8, rectWnd.height() / 4);
// 绘制椭圆
painter.drawEllipse(rectWnd.width() / 2, rectWnd.height() / 2, rectWnd.width() / 4, rectWnd.width() / 8);
// 绘制多边形
painter.setBrush(Qt::NoBrush);
const static QPoint polygon[5] = {QPoint(100,90), QPoint(100,120), QPoint(220,200), QPoint(430,190), QPoint(620,90)};
painter.drawPolygon(polygon, 5);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。