项目中有时需要文字描边的功能
1.基础的绘制文字
使用drawtext处理
void MainWindow::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);painter.setRenderHint(QPainter::SmoothPixmapTransform, true);painter.setRenderHints(QPainter::TextAntialiasing, true);QPen pen = painter.pen();QString m_textString = "文本God123";pen.setWidth(1);pen.setColor(QColor(255, 0, 0));painter.setPen(pen);painter.setBrush(QColor(255, 0, 0));//绘制白芯文字,注意文字的位置点设置painter.drawText(100,100,m_textString);}
2.绘制黑边黄字
代码:
void MainWindow::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);painter.setRenderHint(QPainter::SmoothPixmapTransform, true);painter.setRenderHints(QPainter::TextAntialiasing, true);QPen pen = painter.pen();QString m_textString = "文本God123";QPainterPath path;//注意文字的位置点设置path.addText(100,200,painter.font(), m_textString);pen.setWidth(2);pen.setColor(QColor(0, 0, 0));painter.setPen(pen);//painter.setBrush(QColor(255, 255, 0));//绘制黑边painter.drawPath(path);}
3.只绘制黑边
void MainWindow::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);painter.setRenderHint(QPainter::SmoothPixmapTransform, true);painter.setRenderHints(QPainter::TextAntialiasing, true);QPen pen = painter.pen();QString m_textString = "文本God123";QPainterPath path;//注意文字的位置点设置path.addText(100,200,painter.font(), m_textString);pen.setWidth(2);pen.setColor(QColor(0, 0, 0));painter.setPen(pen);////painter.setBrush(QColor(255, 255, 0));//绘制黑边painter.drawPath(path);
}
4.绘制黑边红字(另一种方法)
void MainWindow::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);painter.setRenderHint(QPainter::SmoothPixmapTransform, true);painter.setRenderHints(QPainter::TextAntialiasing, true);QPen pen = painter.pen();QString m_textString = "文本God123";QPainterPath path;//注意文字的位置点设置path.addText(100,200,painter.font(), m_textString);pen.setWidth(2);pen.setColor(QColor(0, 0, 0));painter.setPen(pen);////painter.setBrush(QColor(255, 255, 0));//绘制黑边painter.drawPath(path);pen.setWidth(1);pen.setColor(QColor(255, 0, 0));painter.setPen(pen);painter.setBrush(QColor(255, 0, 0));//绘制白芯文字,注意文字的位置点设置painter.drawText(100,200,m_textString);
}