项目中有时需要文字描边的功能
使用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);
}
代码:
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);
}
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);
}
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);
}