QTextEdit with a monospaced font - delete char (127) has unusual width



  • OK, this may be very specific so apologies in advance.

    I'm using QTextEdit to display data and to make it monospaced I'm using "Courier New" as a font family.

    All is well except that I wanted to display some symbol to represent an unavailable character. I chose the delete character (ascii 127) which shows a small empty box, but it appears that the width of the character is actually different from the other (printable) characters. Using point size 10, I ran a small loop with QFontMetrics and it appears that the delete character width is 7 while all other chars are 8.

    Shouldn't the monospaced font by definition have the same width for each character?



  • Any ideas, Qt community?



  • I ran some tests and didn't see this. I have two examples:

    courier new point size 10:
    https://www.dropbox.com/s/ubv8ymzilriscde/courier_new-10.png?dl=0

    courier new point size 20:
    https://www.dropbox.com/s/6b6131ok2ndnexk/courier_new-20.png?dl=0

    I used character ascii(1) instead of ascii(127). Ascii(127) is a delete character so is it possible that your text is reduced in size for every one of these used?



  • Thanks for checking Rondog, Could you maybe get short links for the Dropbox urls? They don't seem to work for me, even when I copy paste the entire link into the address bar.



  • I updated the links in the post. Originally they had spaces in the name. Your idea would have worked except I deleted the original, spaced, names.



  • One thing I would like to add that seems to be an obvious omission.

    I tried to run this test using ascii(127) but nothing appeared in QTextEdit (or QPlainTextEdit) for this character. I added some padding text to be sure then switched to ascii(1).

    It is possible there is something different about '127' but I couldn't test this. I didn't check it with QFontMetrics. There are a number of attributes in fonts that might be used to determine the cell width for a character. Maybe anything that displays a box symbol has a combined width of various attributes that is identical for all characters?



  • not really something different, but something missing. It might be that Courier New is missing the character 127, so QFont will search for it in another font. Build a well defined QFont with family name, font name with fixed pitch:

    @
    QFont myFont;
    // font-family [Font name]
    myFont.setFamily("monospace [Courier New]");
    // Tell QFont that the font you're using IS monospaced - QFont will try to render it as monospaced
    myFont.setFixedPitch(true);
    // ask QFont to use a monospaced font when the specified font (in family) is not available
    myFont.setStyleHint(QFont::Monospace);
    @
    Now, every time the specified font is not available, or a character is unot available in the specified font name, QFont will search for it in another monospaced font. However, QFont will not ensure that the character imported from another font is the same size as the others from the specified font. At most, you can use QFont::maxWidth() to find out the width of the widest character available (no character will ever be larger than that).



  • I don't think it works that way. Font substituion is not for individual characters.

    There is no glyph for ascii(127) so displaying a box is suitable. I assume the same happens for all non-assigned characters from any font (I don't know the rules for this sort of thing).

    The cell width for a fixed spaced font must always be the same otherwise it is not a fixed spaced font.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.