坐标缩放导致 QPainter::drawText 中的字体太大
我正在为 MD 模拟代码开发简单的 2D 可视化模块。我想做的是使用以下方法绘制模拟分子的位置:
myPainter.drawEllipse(myQPoint,myRx,myRy)
并且该部分在我的可视化小部件上运行得很好。碰巧出现的问题是编写代表每个分子 ID(整数)的文本。
myPainter.drawText(myPosPoint,QString::number(mySoftMolecule2D->getID()));
它绘制文本,但太大。这可能是因为我需要使用 myPainter 的坐标缩放来轻松绘制分子。
myPainter.scale(myWidgetWidth_ / simSizeX_ , myWidgetHeight_ / simSizeY_);
// myWidgetWidth_ is much bigger simSizeX_
// myWidgetHeight_ is much bigger simSizeY_
我尝试在 myPainter 中执行缩放坐标之前放置这样的线:
QFont myFont;
myFont.setPointSizeF(1.0); // values less than 1.0 doesn't work
myFont.setFamily("Courier");
myPainter.setFont(myFont);
但分子的标签仍然太大。
预先感谢您的任何帮助。
I'm working on simple 2D visualization module for MD simulation code. What I'm trying to do is drawing positions of simulated molecules using:
myPainter.drawEllipse(myQPoint,myRx,myRy)
And that part works pretty good on my visualization widget. The thing that happened to be a problem is writing text which should represent each molecule's ID (integer).
myPainter.drawText(myPosPoint,QString::number(mySoftMolecule2D->getID()));
It draws text but it is too large. This is probably because I need to use cooridantes scaling for myPainter
to draw molecules easily.
myPainter.scale(myWidgetWidth_ / simSizeX_ , myWidgetHeight_ / simSizeY_);
// myWidgetWidth_ is much bigger simSizeX_
// myWidgetHeight_ is much bigger simSizeY_
I tried putting such lines before I perform scaling cooridnates in myPainter
:
QFont myFont;
myFont.setPointSizeF(1.0); // values less than 1.0 doesn't work
myFont.setFamily("Courier");
myPainter.setFont(myFont);
but the molecules' label are still much too big.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您想根据固定像素大小调整字体大小,请尝试使用 QFont:: setPixelSize(int PixelSize) 而不是
setPointSize()
。Since you want to size a font based on a fixed pixel size, try using QFont::setPixelSize(int pixelSize) instead of
setPointSize()
.记住要绘制文本的位置,然后分两个阶段进行绘制。第一个是分子,第二个是文本。在绘制分子之前,保存画家的状态,并在绘制文本之前恢复它。这应该可以防止文本缩放,同时允许缩放分子。
Remember the positions you want to draw the text at, then draw in two stages. The first is the molecules, the second the text. Before drawing the molecules, save the state of the painter, and restore it before drawing the text. This should prevent scaling of the text while allowing the molecules to be scaled.