简体   繁体   English

Alignment 在将 QString 绘制到 QImage 上时出现问题 object

[英]Alignment problems while painting QString onto the QImage object

I want to create a string that will be printed on the screen.我想创建一个将打印在屏幕上的字符串。 I will update this string every time values of the variables printed will change.每次打印的变量值发生变化时,我都会更新此字符串。 The number of digits in each number can be different and sign of each number may change with time.每个数字的位数可以不同,每个数字的符号可能随时间而变化。 What I want to be constant is numbers precision.我想要保持不变的是数字精度。 The output I expect is sth like this (each line correspond to the same string, only updated):我期望的 output 是这样的(每行对应相同的字符串,仅更新):

X:  453.432  Y:  543.432
X:   -5.432  Y:    4.432
X:   43.234  Y: -123.423

Namely, the letters and the dot should always stay in the same place on the screen.即,字母和点应始终位于屏幕上的同一位置。

At the moment I use this code for string updates but position of the Y and dots vary if number of digits change.目前我使用此代码进行字符串更新,但 Y 的 position 和点会随着位数的变化而变化。 Is there a simple way to implement it?有没有简单的方法来实现它?

text = QString("X: %1   Y: %2").arg( QString::number( x(), 'f', 3 ),
                                     QString::number( y(), 'f', 3 ) );

------------------ EDIT ------------------ - - - - - - - - - 编辑 - - - - - - - - -

Following the answer I created a string with constant spacing (constant length actually).按照答案,我创建了一个具有恒定间距(实际上是恒定长度)的字符串。 It didn't solve my problem fully though.虽然它并没有完全解决我的问题。

In the next step I take the string text and using QPainter I paint it onto QImage object.在下一步中,我获取字符串text并使用QPainter将其绘制到QImage object 上。 The problem that I encounter here is that though the string length is always the same the width of numbers is different than the width of spaces (' ') and '-' sign painted by QPainter .我在这里遇到的问题是,尽管字符串长度始终相同,但数字的宽度与QPainter绘制的空格('')和'-'符号的宽度不同。 So when the number of digits changes the positions of dots and 'Y' symbol changes as well.因此,当位数改变点的位置时,“Y”符号也会改变。

Here is the code that I use for image creation:这是我用于创建图像的代码:

QImage img( 100, 100, QImage::Format_ARGB32_Premultiplied ); 
img.fill( Qt::transparent );

QPainter painter( &img );

QPen pen;
pen.setColor( Qt::darkBlue );
painter.setPen(pen);

QFont font = painter.font();
font.setBold( true );
font.setPointSize( 10 );
painter.setFont( font );

QString text = QString("X:%1   Y:%2")
    .arg( x(), 9, 'f', 3, ' ' )
    .arg( y(), 9, 'f', 3, ' ' );
painter.drawText( QPointF(0, 50), text );
painter.end();

You can use this signature of the arg method:您可以使用arg方法的此签名:

// arg(double a, int fieldWidth, char format, int precision, QChar fillChar)
QString formated = QString("X:%1 Y:%2")
    .arg(x(), 9, 'f', 3, ' ')
    .arg(y(), 9, 'f', 3, ' ');

For your second problem, I solve it by changing the font family.对于您的第二个问题,我通过更改字体系列来解决它。

font.setFamily("Courier");

These are my results:这些是我的结果:

Default font:默认字体:
默认字体

Courier font:快递字体:
信使字体

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

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