Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

Accurate font size / painting (Not sure if this is a Qt issue)

$
0
0
Hello, 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 (0×2588 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: QFontMetrics fontMetrics(getFont()); setCellWidth(fontMetrics.width(QChar(0x2588))); setCellHeight(fontMetrics.height()); I’ve also tried this: QFontMetrics fontMetrics(getFont()); setCellWidth(fontMetrics.width(QChar(0x2588))); QFontInfo fontInfo(getFont()); setCellHeight(fontInfo.pixelSize()); and this: QFontMetrics fontMetrics(getFont()); QRect fontBoundingRect = fontMetrics.boundingRect(CHAR_FULL_BLOCK); setCellWidth(fontBoundingRect.width()); setCellHeight(fontBoundingRect.height()); 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.setBrush(getBgColour());  painter.setPen(getBgColour());  painter.drawRect(rect);  painter.setPen(getFgColour());  painter.drawText(rect, Qt::AlignCenter, getCharacterAt(i)); // I've tried aligning to the top and left too, no change. } Thanks.

Viewing all articles
Browse latest Browse all 13965

Trending Articles