设置笔和字体以后,就可以写字了:
void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event); QPainter painter(this);
// 设置画笔颜色
painter.setPen(QColor(, , )); // 设置字体:微软雅黑、点大小50、斜体
QFont font;
font.setFamily("Microsoft YaHei");
font.setPointSize();
font.setItalic(true);
painter.setFont(font); // 绘制文本
painter.drawText(rect(), Qt::AlignCenter, "Qt");
}
设置笔的颜色后,就可以画线了:
void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event); QPainter painter(this);
// 反走样
painter.setRenderHint(QPainter::Antialiasing, true);
// 设置画笔颜色
painter.setPen(QColor(, , ));
// 绘制直线
painter.drawLine(QPointF(, height()), QPointF(width() / , height() / ));
}
设置笔(用于边框)和画刷(用于大面积)后,就可以绘制整块的面积:
void MainWindow::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event); QPainter painter(this); // 反走样
painter.setRenderHint(QPainter::Antialiasing, true);
// 设置画笔颜色、宽度
painter.setPen(QPen(QColor(, , ), ));
// 设置画刷颜色
painter.setBrush(QColor(, , ));
painter.drawRect(, , , );
}
http://blog.csdn.net/liang19890820/article/details/51154216