PDA

View Full Version : Text does not drawn properly in RTOL locale on Mac OS X



sanjayshelke
3rd December 2009, 10:41
Hi all,

I am facing very strange problem with the behavior of QPainter::drawText in RTOL locale on Mac OS X.

If i draw any text by providing bounding rect and specifying the alignment like drawText( rt, Qt::AlignLeft, text), it does not drawn properly.

ANy one knows why this is happening?


Thanks in advance.

regards,
~Sanjay

wysota
3rd December 2009, 10:56
What does "not drawn properly" mean in this case?

sanjayshelke
3rd December 2009, 11:19
I have customized QGraphicsItem to draw the custom fractions like "1000 1/2".
SO in RTOL locale this fraction should be displayed as "1/2 1000".
In Paint() i written code to draw the 1000 to the left of the bounding rect and 1/2 to the right of the bounding rect.
This is working properly on Windows in RTOL or LTOR locale.

But on Mac OS X these texts are getting overlapped on each other in RTOL locale.

wysota
3rd December 2009, 11:29
Can we see the code?

sanjayshelke
3rd December 2009, 12:03
I have attached the sources. please have a look.

wysota
3rd December 2009, 12:55
To me it seems the layout of text has nothing to do with the effect you get. As you are drawing everything yourself, Qt can't decide on anything. The only thing it could do (I don't say it does) is to reverse the meaning of right and left when calling QPainter::drawText() but this is easy to check - simply reverse the flag in your code when doing RTOL on a Mac and see if it helps. If it's still wrong then you're probably calculating positions incorrectly. It also depends on what "overlapping" means in your case.

sanjayshelke
3rd December 2009, 13:49
I tried with reversing the flag in the code when doing RTOL on a Mac, but it does not work.
The text gets overlapped means
1/2 and
1000 gets overlapped.

i guess there is problem while drawing text with the alignment flags on Mac OS X in RTOL locale.

As far as their positions are concerned their calculations are straightforward. I dont think so there is any problem.

The most notable thing is that this works fine on windows. I agree that the windows architecture is different but then the question is this problem should be there on windows for RTOL if the calculation is wrong.

wysota
3rd December 2009, 13:58
The text gets overlapped means and gets overlapped.

Yes, that explains a lot :)

Are they drawn in exactly the same place or does only parts cover the same region? Maybe you could post a screenshot?

sanjayshelke
3rd December 2009, 16:19
Sorry that was a typo mistake.

I have attached two screen shots for your reference. One screen shot contains overlapped text and other is required text.


Please have a look

wysota
3rd December 2009, 17:22
To me it seems your item may simply be too narrow. Try increasing the boundingRect() and see if it changes anything.

sanjayshelke
3rd December 2009, 17:36
Increasing the bounding rect does solve my problem.
BUt then this is the incorrect way to fix this issue. I am just finding out why this is been incorrect in RTOL locale.

If you can see the implementation of boundingRect() of this item, it has been implemented properly as far as my knowledge is concerned.

see this :

QRectF QFractionTextItem :: boundingRect()const
{
return QRectF( 0, 0 ,m_nWidth, m_nHeight*2 );
}

where m_nWidth is the addition of QFontMetrics::width("1000") and qMax( QFontMetrics::width("1") , QFontMetrics::width("2") )

SO i am just wondering what is going wrong behind the screen.

wysota
3rd December 2009, 17:54
BUt then this is the incorrect way to fix this issue.
True. The correct way is to fix your code which apparently computes the bounding rect incorrectly.


where m_nWidth is the addition of QFontMetrics::width("1000") and qMax( QFontMetrics::width("1") , QFontMetrics::width("2") )

Maybe there is some margin somewhere? Try adding this code to your paint():


painter->save();
painter->setPen(Qt::red);
painter->drawRect(boundingRect());
painter->restore();

Also you don't tell the item that its bounding rect changes when you change the font. You should call prepareGeometryChange() after setting the font.

sanjayshelke
3rd December 2009, 18:09
I tried this option of drawing rectangle around its bounding rect, but there is no margin.

The most important thing i like to point out that if you draw the text by specifying the x and y pos( do not provide rectt and alignment while drawing text ), it does draws the text properly without getting overlapped on each other.

But even though i have implemented the boundingRect() method correctly, it clips some part of the text from outside.

here are some points i have highlighted
1.QFontMetrics is not working properly and hence giving incorrect width of the text
2.boundingRect() is incorrect ( which looks like the root cause but its implemenation is very straight forward and correct )
3.QPainter::drawText() with the parameter rect and alignment flags behaves incorrectly in RTOL locale on Mac. ( this is sure a Qt bug )

wysota
3rd December 2009, 22:57
(1) is impossible - it wouldn't work in many other cases as well and apparenty it does work otherwise every output in Qt would be a mess
(2) may only be your own fault
(3) is possible although if I were you I would refrain from using the word "surely" until you look at source code of the method and prove it is a bug. Right now I am closer to proving it is not a bug in Qt than you are to proving it is. At least I have taken a look into the source code and I can say there is no ifdef for mac in code responsible for drawing the text through your method.

sanjayshelke
4th December 2009, 07:41
ok. I agreed that i have not taken a look on the methods i have used. I do not have a source code of Qt. I will take it first.
But then i am still unable to find out the cause of this weird behavior.
What you suggest ion this?

sanjayshelke
4th December 2009, 09:02
Based on the points i have highlighted i troubleshoot the issue.
Further debugging the issue i could found some fruitful observations.
Observation are:

font used in the class is "Comic Sans Ms" and point size is 18
1.in LTOR locale
QFontMetrics::width("1000") returns 41 value
QFontMetrics::width("900") returns 33 value
QFontMetrics::width("90") returns 22 value

2.in RTOL locale
QFontMetrics::width("1000") returns 30 value
QFontMetrics::width("900") returns 22 value
QFontMetrics::width("90") returns 22 value


it looks that QFontMetrics::width() gives incorrect values on Mac OS X in RTOL locale.

You can also try out this by just drawing "1000" in your customized QGraphicsItem on Mac OS X in RTOL and LTOR locale. Just drawing text "1000" gets clipped.

wysota
4th December 2009, 09:50
Did you try other fonts as well? Maybe the font itself is broken...

sanjayshelke
4th December 2009, 10:29
Yes i have tried with other fonts also. But the problem still exist.

wysota
4th December 2009, 10:43
Run one of existing Qt applications (like Designer) with the -reverse parameter. See if widths of text (especially in message boxes where the texts are long) got screwed up.

sanjayshelke
4th December 2009, 11:26
Which message box your talking about in designer app? Can you please elaborate?
I can see none of the text displayed on designer app is screwed up.

wysota
4th December 2009, 12:13
Which message box your talking about in designer app?
Any :)


I can see none of the text displayed on designer app is screwed up.

So font metrics should be calculated properly. Otherwise sizeHint()s of labels would be incorrect and labels and layouts would be squeezed. I'm still convinced the problem is not there in QFontMetrics.

sanjayshelke
4th December 2009, 14:50
This may not be correct way to see the designer app for verifying the said problem.
It is better to try out this with customizing the QGraphicsItem to draw the "1000" number.
If it is not too much headache for you can you please try it.

wysota
4th December 2009, 15:29
This may not be correct way to see the designer app for verifying the said problem.
Why not?


It is better to try out this with customizing the QGraphicsItem to draw the "1000" number.
If it is not too much headache for you can you please try it.

I don't have a Mac, so this won't do you any good.

sanjayshelke
4th December 2009, 16:30
May be designer app does load qt_ar.qm or qt_he.qm

I have written sample app which has customized the qgraphicsItem to draw "1000 1/2".
I have also written one more application which has customized QWidget to draw "1000 1/2".

And both has same problem on Mac OS X in RTOL locale.

If there is any problem in my code then this should not work on windows in any locale.


Please have a look on the sample applications i have attached here.

wysota
4th December 2009, 16:42
If there is any problem in my code then this should not work on windows in any locale.

That's not true. Some behaviour might be different on both systems and this difference might cause your problem not to reveal itself. If you are convinced this is a bug then simply report it to the Trolls - your QFontMetrics example should do the trick.

sanjayshelke
4th December 2009, 16:56
ok. Really thanks for you time wysota.
I will report the bug to Trolltech then.