Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QT inserting multiple fonts on windows

QT inserting multiple fonts on windows

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 1 Posters 288 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.
  • B Offline
    B Offline
    Beckett37
    wrote on 11 Feb 2023, 09:23 last edited by
    #1

    I have a PyQT program that saves a file in HTML format.

    It uses the QFontComboBox and on a change in the font selection I set the application's editor (QTextEdit) to match the selection.

    self.fonts = QFontComboBox()
    self.fonts.currentFontChanged.connect(self.setSelectedFont)
     
    def setSelectedFont(self, font):
            logging.debug(
                "setSelectedFont: font has changed: setting the editor font to {}".format(font.toString()))
            self.editor.setCurrentFont(font)
    

    Additionally, if the user selects some text in the editor it updates the QFontComboBox with the font on the selected text.

    currentFont = self.editor.currentFont()
    self.fonts.setCurrentFont(currentFont) # where self.fonts is a QFontComboBox
    

    On Ubuntu this works fine and I can save the resulting HTML file as follows:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
    <html><head><meta name="qrichtext" content="1" /><style type="text/css">
    p, li { white-space: pre-wrap; }
    </style></head><body style=" font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;">
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus'; font-size:12pt;">This is Papyrus</span></p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:''Playball'; font-size:12pt;">This is Playball</span></p>
    <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Papyrus'; font-size:12pt;"><br /></p></body></html>
    

    However, on windows 11 I get the following

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
    <html><head><meta name="qrichtext" content="1" /><style type="text/css">
    p, li { white-space: pre-wrap; }
    </style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus'; font-size:12pt;">This is Papyrus</span></p>
    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus','Playball'; font-size:12pt;">This is Playball</span></p>
    <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Papyrus'; font-size:12pt;"><br /></p></body></html>
    

    The problem is that on windows it is sometimes adding two font types as in: font-family:'Papyrus','Playball';

    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus','Playball'; font-size:12pt;">This is Playball</span></p>
    

    Even when I create a new document in the editor and add a single line with the font already selected as "permanent marker", I get the following:

    <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus','Permanent Marker'; font-size:12pt;">This is permanent marker</span></p></body></html>
    

    It adds Papyrus, a font I had used in a previous document in front of the desired front Permanent Marker.

    I cannot figure out why it is doing this. Any suggestions?

    B 1 Reply Last reply 12 Feb 2023, 18:47
    0
    • S SGaist moved this topic from Language Bindings on 11 Feb 2023, 19:52
    • B Beckett37
      11 Feb 2023, 09:23

      I have a PyQT program that saves a file in HTML format.

      It uses the QFontComboBox and on a change in the font selection I set the application's editor (QTextEdit) to match the selection.

      self.fonts = QFontComboBox()
      self.fonts.currentFontChanged.connect(self.setSelectedFont)
       
      def setSelectedFont(self, font):
              logging.debug(
                  "setSelectedFont: font has changed: setting the editor font to {}".format(font.toString()))
              self.editor.setCurrentFont(font)
      

      Additionally, if the user selects some text in the editor it updates the QFontComboBox with the font on the selected text.

      currentFont = self.editor.currentFont()
      self.fonts.setCurrentFont(currentFont) # where self.fonts is a QFontComboBox
      

      On Ubuntu this works fine and I can save the resulting HTML file as follows:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
      <html><head><meta name="qrichtext" content="1" /><style type="text/css">
      p, li { white-space: pre-wrap; }
      </style></head><body style=" font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;">
      <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus'; font-size:12pt;">This is Papyrus</span></p>
      <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:''Playball'; font-size:12pt;">This is Playball</span></p>
      <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Papyrus'; font-size:12pt;"><br /></p></body></html>
      

      However, on windows 11 I get the following

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
      <html><head><meta name="qrichtext" content="1" /><style type="text/css">
      p, li { white-space: pre-wrap; }
      </style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;">
      <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus'; font-size:12pt;">This is Papyrus</span></p>
      <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus','Playball'; font-size:12pt;">This is Playball</span></p>
      <p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Papyrus'; font-size:12pt;"><br /></p></body></html>
      

      The problem is that on windows it is sometimes adding two font types as in: font-family:'Papyrus','Playball';

      <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus','Playball'; font-size:12pt;">This is Playball</span></p>
      

      Even when I create a new document in the editor and add a single line with the font already selected as "permanent marker", I get the following:

      <p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Papyrus','Permanent Marker'; font-size:12pt;">This is permanent marker</span></p></body></html>
      

      It adds Papyrus, a font I had used in a previous document in front of the desired front Permanent Marker.

      I cannot figure out why it is doing this. Any suggestions?

      B Offline
      B Offline
      Beckett37
      wrote on 12 Feb 2023, 18:47 last edited by Beckett37 2 Dec 2023, 18:47
      #2

      @Beckett37

      I have worked around the issue for now with an ugly hack

      
      def removeMultipleFonts(self):
              html = self.editor.document().toHtml()
              htmlTransformed = re.sub(r"(font-family:)('.+'),('.+')", r'\1\3', html)
              self.editor.document().setHtml(htmlTransformed)
      

      Is seems to resolve the issue. I invoke this on changing font and saving the file.

      1 Reply Last reply
      0

      2/2

      12 Feb 2023, 18:47

      • Login

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