我能否让QPainter字体与其他所有单位一样使用相同的单位? [英] Can I make QPainter fonts operate in the same units as everything else?

查看:525
本文介绍了我能否让QPainter字体与其他所有单位一样使用相同的单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  void draw_text(QPainter& p,const QString& text,QRectF target)
{
float scale = calculate_font_scale(p,text,target); //大约0.0005

QFont f = p .font();
float old_size = f .pointSizeF();
f .setPointSizeF(old_size * scale);
p .setFont(f);

//这将正确输出新的字体大小
qWarning(old:%f,new:%f,old_size,p .font().pointSizeF());

//但是这似乎并没有影响到所有
p .drawText(position,text);



$ b

QPainter的字体大小已被正确更新,因为qWarning行表明,但是文字很大,很大。我认为这是因为QPainter坐标系已经放大了很多,并且似乎setPointSizeF只适用于至少1的大小。看来,这个字体似乎是一个单位高,所以我会买这个解释,虽然它是愚蠢的。



我尝试使用 setPixelSize 代替,虽然 p.fontMetrics()。boundingRect(text)产生了一个看起来很明智的答案,它在像素单元上述函数的一个要求是,文本的边界矩形相对于 target 参数是水平和垂直居中的,坐标的尺寸大不相同,所以算术不再有效,文本在屏幕外被绘制出来。

我希望能够任意地转换坐标系如果在这一点上,一个单位是一个千像素高,我正在0.03x0.03单位框中绘制文本,那么我想字体是30像素高,显然,但我需要我所有的几何一般以单位计算,而且我需要 fontMetrics :: boundingRect 在这些相同的单位中。



<有没有什么方法可以解决这个问题,或者我必须使用像素计算来解决字体API问题?

解决方案

你只需要撤销画家的任何疯狂缩放。

  //保存状态
p.save();
//将target的中心转换为0,0。
p.translate(-target.center());
//缩放以便目标具有合理的大小
qreal dim = 256.0;
qreal sf = dim / qMin(target.height(),target.width());
p.scale(sf,sf);
//绘制文字
p.setPointSize(48);
p.drawText(QRectF(dim,dim),Qt :: AlignCenter | Qt :: WordWrap,text);
//恢复状态
p.restore();


I started with this

void draw_text (QPainter & p, const QString & text, QRectF target)
{
    float scale = calculate_font_scale (p, text, target); // about 0.0005

    QFont f = p .font ();
    float old_size = f .pointSizeF ();
    f .setPointSizeF (old_size * scale);
    p .setFont (f);

    // this prints the new font size correctly
    qWarning ("old: %f, new: %f", old_size, p .font () .pointSizeF ());

    // but that doesn't seem to affect this at all
    p .drawText (position, text);
}

The QPainter's font has size has been correctly updated, as the qWarning line indicates, but the text draws much, much to big. I think this is because the QPainter coordinate system has been zoomed-in quite a lot and it seems setPointSizeF only works with sizes of at least 1. By eye it seems that the font is one "unit" high so I'll buy that explanation, although it's stupid.

I experimented with using setPixelSize instead, and although p.fontMetrics().boundingRect(text) yields a sane-looking answer, it is given in pixel units. One requirement for the above-function is that the bounding rect of the text is horizontally and vertically centred with respect to the target argument, which is in coordinates of a vastly different scale, so the arithmetic is no longer valid and the text is drawn miles off-screen.

I want to be able to transform the coordinate system arbitrarily and if, at the point, one "unit" is a thousand pixels high and I'm drawing text in a 0.03x0.03 unit box then I want the font to be 30 pixels high, obviously, but I need all my geometry to be calculated in general units all the time, and I need fontMetrics::boundingRect to be in these same general units.

Is there any way out of this or do I have to dick around with pixel calculations to appease the font API?

解决方案

You simply have to undo whatever "crazy" scaling there was on the painter.

// Save the state
p.save();
// Translate the center of `target` to 0,0.
p.translate(-target.center());
// Scale so that the target has a "reasonable" size
qreal dim = 256.0;
qreal sf = dim/qMin(target.height(), target.width());
p.scale(sf, sf);
// Draw your text
p.setPointSize(48);
p.drawText(QRectF(dim, dim), Qt::AlignCenter | Qt::WordWrap, text);
// Restore the state
p.restore();

这篇关于我能否让QPainter字体与其他所有单位一样使用相同的单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆