简体   繁体   English

QGraphicsView :: drawForeground上的drawText

[英]drawText on QGraphicsView::drawForeground

My code is below. 我的代码如下。

void MyView::drawForeground(QPainter *painter, const QRectF &rect)
{
    Q_UNUSED(rect);

    painter->save();

    QRectF rt = viewport()->rect();
    painter->setWorldMatrixEnabled(false);

    QString strInfo = "test12345";

    painter->setBrush(Qt::NoBrush);
    painter->setPen(Qt::white);

    painter->setFont(QFont("Segoe UI", 200, QFont::Bold));

    QFontMetrics fm(painter->font());
    int fmWidth = fm.width(strInfo, strInfo.length());
    int fmHeight = fm.height();

    painter->drawRect(rt);
    painter->drawText(10, 10, fmWidth, fmHeight, Qt::AlignCenter, strInfo);
    painter->drawRect(10, 10, fmWidth, fmHeight);
    painter->setWorldMatrixEnabled(true);
    painter->restore();
}

result image 结果图片

QfontMetrics does not seem to return the correct size. QfontMetrics似乎没有返回正确的大小。 What's wrong with my code? 我的代码有什么问题?

I do not know what to do anymore. 我不知道该怎么办了。

QFontMetrics returns the size of the white rectangle of the attached image, but the actual Text is drawn small. QFontMetrics返回附加图像的白色矩形的大小,但实际的Text绘制得很小。

不管QPainter::setWorldMatrixEnabled(false)QPainter::drawText()充当场景坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM