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] QPlainTextEdit: how to change the font to be monospaced?

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 28.1k 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.
  • J Offline
    J Offline
    John.Curious
    wrote on 30 Dec 2013, 04:20 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 30 Dec 2013, 05:47 last edited by
      #2

      @
      #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();
      

      }
      @

      1 Reply Last reply
      0
      • J Offline
        J Offline
        John.Curious
        wrote on 30 Dec 2013, 19:53 last edited by
        #3

        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?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          John.Curious
          wrote on 30 Dec 2013, 22:39 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on 31 Dec 2013, 01:59 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kenchan
              wrote on 31 Dec 2013, 02:30 last edited by
              #6

              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.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                John.Curious
                wrote on 2 Jan 2014, 04:49 last edited by
                #7

                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 ...

                1 Reply Last reply
                0

                7/7

                2 Jan 2014, 04:49

                • Login

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