QT inserting multiple fonts on windows
-
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?
-
S SGaist moved this topic from Language Bindings on
-
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?
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.