3

I want to do a fairly simple drawing where I write a two pieces of text above each other in the center of the circle. My code:

    painter->drawText(QRectF(0, 0, m_iSize, m_iSize), Qt::AlignCenter, m_sAlias + "\n" + m_sCode);

where m_iSize is the size of the circle, m_sAlias is a short text like "R1" and m_sCode is another short text like "31".

The problem is that the above code will draw both lines of text so that they are exactly ONE pixel apart horizontally. And unfortunately it is clearly visible. I suspect the "\n" must do something to it but what I do not know. Nor how to solve it.

My current workaround is to draw the texts separately like this:

    painter->drawText(QRectF(1, 0, m_iSize, m_iSize), Qt::AlignCenter, m_sAlias + "\n");
    painter->drawText(QRectF(0, 0, m_iSize, m_iSize), Qt::AlignCenter, "\n" + m_sCode);

But that is just stupid even though it actully works as expected (I am shifting the top text one pixel to the right).

What am I missing here? If need be I can provide screenshots.

Screenshots:

Wrong (the first one line code), lines are moved by one pixel from each other

Correct (the second two-lines code), lines are aligned correctly

Resurrection
  • 3,916
  • 2
  • 34
  • 56
  • It'd be nice to have a screenshot. – marius_linux Sep 04 '15 at 18:47
  • On which platform and in what size, font ... do you do this btw? I'm failing at reproducing this. – marius_linux Sep 04 '15 at 19:02
  • @marius_linux Added some screenshots (you may need to zoom them up to see it more clearly but it is definitely visible even at this size, especially in comparison). I am using Qt 5.5 with MingW (32-bit) on Windows 8 64-bit. I do not set any fonts, sizes or whatever so it is what the defaults are. I am using Qt's fusion style for the application if that matters. – Resurrection Sep 04 '15 at 20:34

1 Answers1

4

I cannot reproduce this problem, but I am on different system with different default font. It could be that the font you are using specifies 1 px width for the new line character, or Qt is misinterpreting it like so. You should definitely try what happens with another font.

Anyway, you could use this workaround instead:

painter->drawText(QRectF(0, 0, m_iSize, m_iSize), Qt::AlignCenter | Qt::AlignTop, m_sAlias);
painter->drawText(QRectF(0, 0, m_iSize, m_iSize), Qt::AlignCenter | Qt::AlignBottom, m_sCode);

It will render correctly no matter if you are on system that has the new line problem or system that doesn't.

michalsrb
  • 4,703
  • 1
  • 18
  • 35
  • 1
    Thanks for the suggestion. There is just one type, it needs Qt::AlignHCenter because Qt::AlignCenter counts as both vertical and horizontal flag and you can have only one of each applied. Anyway, to my surprise this code produced the exactly same problem for me, the lines are one pixel misaligned! I have also tried different fonts and you were right in that it is font specific because some fonts do produce this problem and some don't. I am really confused but at least I know how to deal with this now. – Resurrection Sep 05 '15 at 08:45
  • In the end I went for embedded widgets since I also needed the text editing capabilities and QGraphicsText class did not really cut it for me (i.e. no signals for text editing) but this was important lesson none the less. – Resurrection Sep 07 '15 at 17:45