设置笔和字体以后,就可以写字了:
void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // 设置画笔颜色 painter.setPen(QColor(0, 160, 230)); // 设置字体:微软雅黑、点大小50、斜体 QFont font; font.setFamily("Microsoft YaHei"); font.setPointSize(50); 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(0, 160, 230)); // 绘制直线 painter.drawLine(QPointF(0, height()), QPointF(width() / 2, height() / 2)); }
设置笔(用于边框)和画刷(用于大面积)后,就可以绘制整块的面积:
void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // 反走样 painter.setRenderHint(QPainter::Antialiasing, true); // 设置画笔颜色、宽度 painter.setPen(QPen(QColor(0, 160, 230), 2)); // 设置画刷颜色 painter.setBrush(QColor(255, 160, 90)); painter.drawRect(50, 50, 160, 100); }
http://blog.csdn.net/liang19890820/article/details/51154216