Python Forum
[PyQt] PyQT Problems with multiple fonts - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyQt] PyQT Problems with multiple fonts (/thread-39391.html)



PyQT Problems with multiple fonts - DrakeSoft - Feb-10-2023

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

It uses the QFontComboBox and on a change in selection I set the 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:


Quote:<!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


Quote:<!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';


Quote:<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:

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

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


RE: PyQT Problems with multiple fonts - DrakeSoft - Feb-12-2023

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.


RE: PyQT Problems with multiple fonts - wilkinsonwilfrid - Feb-21-2024

(Feb-12-2023, 06:39 PM)DrakeSoft Wrote: 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)
geometry dash

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

That is the problem that I am having as well. I do not know how to find a solution to this problem. It was you who provided the solution. The problem has been repaired by me. I am grateful to you.