[Interest] Font measuring inconsistency between platforms in Qt5? Or, how best to draw characters with QPainter.

George Tasopoulos tasopoulos.yorghos at gmail.com
Sun Dec 29 18:16:29 CET 2013


Hello everyone,

I'm using Qt5 in Linux (Mint 16) and Windows (7). In both cases it has been
downloaded and installed from the Qt Project website (not the package
manager). The following code produces different results on each system:

QFont font("Liberation Mono", 18, QFont::Normal, false);
QFontMetrics metrics(font);
int x = metrics.height();

On Windows x equals 27, which seems to be the correct value, but on Linux
it equals 28. The font file used by the two systems is the same one (I've
even done a binary comparison which found no differences).

What I'm trying to do is create a widget that displays a grid of characters
with no gaps between them. The size of the cells in the grid is decided by
QFontMetrics.height() and QFontMetrics.width(QChar(0x2588)).

(0x2588 is the "Full Block" character in UTF-16, which is supposed to take
up the full width and height of the character. Unfortunately many fonts
don't implement it correctly, such as Vera Sans Mono, which has a large gap
at the top).

If I fill my grid with Full Block characters, it appears correctly in
Windows but when run in Linux there are 1-pixel-wide gaps between each line.

Is this a bug in Qt or are the results produced by QFontMetrics not certain
to be the same across platforms? If it's the latter case, is there a more
accurate way to determine what size each cell in my grid should be?

If it's any help, this is how I draw my characters:

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.
// x is the horizontal position of the current cell.
    int y = i / getGridWidth(); // y is the vertical position of the
current cell.
    QRect rect(x * getCellWidth(), y * getCellHeight(), getCellWidth(),
getCellHeight());
    painter.setBrush(getBgColour());
    painter.setPen(getBgColour());
    painter.drawRect(rect);
    painter.setPen(getFgColour());
    painter.drawText(rect, Qt::AlignBottom | Qt::AlignHCenter,
QChar(0x2588)); // I've tried aligning to the top and left too, no change.
}

I would be grateful for any advice.
--
Yours sincerely,
George Tasopoulos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131229/ce8d3d1e/attachment.html>


More information about the Interest mailing list