views:

24

answers:

2

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.

A: 

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

Arnold Spence
A: 

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.

Caleb Huitt - cjhuitt