New QTextBlockFormat from QTextDocument 's StyleSheet
Unsolved
General and Desktop
-
@Jim-Gir
I'm lost. There is noQTextDocument::setStyleSheet()
.QTextDocument
is not aQWidget
. There is aQTextDocument::setDefaultStyleSheet()
, but that is not what you say you are using. So how do you get your code to compile/run withmydoc.setStyleSheet()
? -
Here is a snippet (it's python I don't think it changes anything)
from PySide2.QtGui import QTextDocument, QTextCursor, QTextBlockFormat, QTextCharFormat, QBrush, QColor a = QTextDocument() a.setDefaultStyleSheet("""h2 {margin-top: 20px;}""") a.setHtml("") bf = QTextBlockFormat() bf.setHeadingLevel(2) print(bf.topMargin()) # 0.0 c = QTextCursor(a) c.insertBlock(bf) print(a.toHtml()) # <body style=" font-family:''; font-weight:400; font-style:normal;"> # <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;"><br /></p> # <h2 style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></h2></body></html>
in this example
h2 margin-top
is never set as I expect -
Ok I see.
inserthtml
was my first try before usinginsertblock
. But using It I can't add new block.a = QTextDocument() a.setDefaultStyleSheet("""h2 {margin-top: 20px;} p {margin-top: 15px;}""") a.setHtml("""<p>bbb</p>""") c = QTextCursor(a) c.movePosition(QTextCursor.End) c.insertHtml("<h2>bla</h2>") print(a.toHtml()) #<html><head><meta name="qrichtext" content="1" /><style type="text/css"> #p, li { white-space: pre-wrap; } #</style></head><body style=" font-family:''; font-weight:400; font-style:normal;"> #<p style=" margin-top:15px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">bbb<span style=" font-size:x-large; font-weight:600;">bla</span></p></body></html>