Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Accurate font size / painting (Not sure if this is a Qt issue)
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zalbor
    wrote on last edited by
    #1

    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 (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:
    @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.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      Zalbor
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        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)

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          Zalbor
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You're welcome !

            If you can get further with this matter, don't forget to update this thread

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              Zalbor
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved