Coordinate scaling causes too large font sizes in QPainter::drawText

∥☆過路亽.° 提交于 2019-12-10 23:05:13

问题


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.


回答1:


Since you want to size a font based on a fixed pixel size, try using QFont::setPixelSize(int pixelSize) instead of setPointSize().




回答2:


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.



来源:https://stackoverflow.com/questions/3949877/coordinate-scaling-causes-too-large-font-sizes-in-qpainterdrawtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!