Python Forum
[PyQt] PyQT Problems with multiple fonts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] PyQT Problems with multiple fonts
#1
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?
Reply
#2
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.
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] proportional fonts DPaul 3 1,248 Aug-18-2022, 05:43 AM
Last Post: DPaul
  [Tkinter] Custom Fonts GalaxyCoyote 1 4,703 Dec-25-2019, 06:56 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020