[SOLVED] Accurate font size / painting (Not sure if this is a Qt issue)
I'm trying to create a custom widget (by overloading the paint event) which displays a grid of characters, with a foreground and background colour. To do this, for each cell in the grid I paint a rectangle with the background colour and then draw the relevant character on top, centred, with the foreground colour.
In order to determine the size of each cell in this grid, I use the full block character (0x2588 in UTF-16), which is supposed to fill the entire space of the character. I've tried with the DejaVu Sans Mono and Bitstream Vera Sans Mono fonts.
Using QFontMetrics.width() with this character seems to return the correct width: filling the grid with full block characters shows no vertical lines between them. However I can't seem to make the height of each cell to be right. If I use QFontMetrics.height() to determine the height, I see horizontal lines between the characters. If I use QFontInfo.pixelSize() the horizontal lines are still exist.
As I said in the title, I'm not sure if this is the Qt classes returning the wrong values. It could be that the fonts themselves don't implement the full block character correctly, but I'm not sure how to test that.
In case it helps, I'll attach some relevant pieces of code.
This is how I determine cell sizes:
I've also tried this:
QRect fontBoundingRect = fontMetrics.boundingRect(CHAR_FULL_BLOCK);
This is how the cells are painted (in paintEvent):
@for(int i = 0; i < gridSize; ++i) //gridSize is the number of cells in the grid
int x = i % getGridWidth(); // the grid width is the number of cells in a line
int y = i / getGridWidth();
QRect rect(x * getCellWidth(), y * getCellHeight(), getCellWidth(), getCellHeight());
painter.drawText(rect, Qt::AlignCenter, getCharacterAt(i)); // I've tried aligning to the top and left too, no change.
The simplest workaround I've found so far is to align the characters with the bottom (Qt::AlignBottom | Qt::AlignHCenter) and use a constant to make the height of each cell smaller, in order to remove the gap from the top of the character.
I also found that the full block character at size 18 (points) is 21 pixels high, but QFontInfo.pixelSize() returns 24, which is exactly the distance between the bottom of one full block and the bottom of the full block above it, however that still doesn't show if the font isn't correct or I'm using Qt wrong.
The topic is interesting but I'm not sure you'll reach the audience you'd like here. You should bring this on the interest mailing list, you'll reach Qt's developers/maintainers there (this forum is more user oriented)
Thanks for the advice. I concluded it was a font issue after all (Liberation Mono does fill the entire space) but there's still an inconsistency between platforms, so I'll take that to the mailing list.
You're welcome !
If you can get further with this matter, don't forget to update this thread
Well, there were actually two problems.
One was that the font I was using, contrary to what I had read elsewhere, didn't implement the Full Block character correctly but had a gap at the top.
The other problem was that I hadn't understood how to use QPainter well enough: Using drawRect() to clear each cell made it also draw a border around it, which erased some pixels from nearby cells. Replacing it with fillRect() ensures it only paints pixels inside the given rectangle.
For drawRect, you can set the painter's pen width to zero and you should not have this border. But you are right, in your case fillRect is the way to go