[SOLVED] QPlainTextEdit: how to change the font to be monospaced?



  • Hello gurus, I am sorry to ask what would seem to be a simple question, but I tried to use setFont and setCurrentCharFormat in many different ways, but the font used in the QPlainTextEdit widget does not change.

    I want it to be monospaced, and let system to choose the rest of font attributes by its default.

    Could somebody point me to a sample of code which uses monospaced font in QPlainTextEdit?

    Thanks in advance!



  • @
    #include <QtGui>

    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);

    QPlainTextEdit monoEdit;
    QTextDocument *doc = monoEdit.document();
    QFont font = doc->defaultFont();
    font.setFamily("Courier New");
    doc->setDefaultFont(font);
    monoEdit.setPlainText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mattis odio eget lectus imperdiet sollicitudin.");
    
    monoEdit.show();
    return app.exec();
    

    }
    @



  • Thank you very much, this works, but I was trying to avoid using specific font names and let system to choose anything available monospaced and reasonable.

    Is there a way to set a monospaced font without specifying a particular font name? I was thinking that setFixedPitch(true) should make it choose a suitable monospaced font, but it does not. Am I trying to do something that is not implemented?

    I also could not find in the QT documentation what font names are typically available? Yes, "Courier" seems to be one of them, but what if I do not care which font it is, as long as it is monospaced? How do I get a list of available monospaced fonts, if I have to use some specific font name?



  • Well, I played a bit more with it and found out that the code below does the trick I wanted:
    @
    QPlainTextEdit monoEdit;
    QFont f("unexistent");
    f.setStyleHint(QFont::Monospace);
    monoEdit.setFont(f);
    @

    (BTW, setFont works fine on QPlainTextEdit, it does not have to be on QTextDocument.)

    This is strange to me that one has to specify a bogus font name to get it resolved. If I use QFont's default constructor, or anything else I tried, it does not change the font to be monospaced.

    I wish there would be a more elegant solution. There should be a way to invalidate currently resolved family name and make it to resolve again with newer hints specified. I tries QFont::resolve(0) to invalidate the current choices, but it did not help.

    Does anyone have an idea?

    Is font family name "Courier" guaranteed to be monospaced and available on any platform?



  • Courier, Times and Helvetica are some of the default Postscript fonts installed on printers and have become more-or-less universal generic family names (take a look at any web page/css source).

    The name "Courier" will be mapped to a operating system font (rules in QFont docs). Since the generic Courier is a monospaced font then the mapped result is very likely to be monospaced also, even if there is no literal "Courier" available. On my Linux box "Courier" maps to "Nimbus Mono L". On Windows it will give you "Courier New" according to the QFont docs.

    To get your system's generic monospaced font I would try:
    @
    QFont font("monospace");
    QFontInfo info(font);
    qDebug() << font << info.family() << info.fixedPitch();
    @
    and let the system's default mapping get selected.

    You can, if you wish, iterate over the families returned by QFontDataBase::families looking for one with isFixedPitch() == true.



  • Hi, Don't forget to change the title of your post to [solved] if you are happy with the solution offered.

    Your issue looks like it will be very useful for others.

    Thanks.



  • Yes, I was looking for a button how to mark an issue "solved". So, I need to simply manually edit the title? I missed that instruction somewhere ...


Log in to reply
 

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