Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. New QTextBlockFormat from QTextDocument 's StyleSheet
Forum Updated to NodeBB v4.3 + New Features

New QTextBlockFormat from QTextDocument 's StyleSheet

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.1k 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.
  • J Offline
    J Offline
    Jim Gir
    wrote on last edited by
    #1

    Hi,
    If I specify mydoc.setStyleSheet(some css), and I create a new QTextBlocFormat the formatting of the new blockFormat is not the one specified in stylesheet.
    How can I do it ?
    Thanks
    Jimmy

    JonBJ 1 Reply Last reply
    0
    • J Jim Gir

      Hi,
      If I specify mydoc.setStyleSheet(some css), and I create a new QTextBlocFormat the formatting of the new blockFormat is not the one specified in stylesheet.
      How can I do it ?
      Thanks
      Jimmy

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Jim-Gir
      I'm lost. There is no QTextDocument::setStyleSheet(). QTextDocument is not a QWidget. There is a QTextDocument::setDefaultStyleSheet(), but that is not what you say you are using. So how do you get your code to compile/run with mydoc.setStyleSheet()?

      1 Reply Last reply
      1
      • J Offline
        J Offline
        Jim Gir
        wrote on last edited by
        #3

        oh sorry, I meant setDefaultStyleSheet(). And that's my problem. this should set a default style but creating new QTextBlocFormat doesn't seet style like in my custom css.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jim Gir
          wrote on last edited by
          #4

          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

          JonBJ 1 Reply Last reply
          0
          • J Jim Gir

            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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Jim-Gir
            Test out using

            for example using setHtml() or QTextCursor::insertHtml().

            and see whether these do work? https://doc.qt.io/qt-5/qtextdocument.html#defaultStyleSheet-prop only states these work.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jim Gir
              wrote on last edited by
              #6

              Ok I see. inserthtml was my first try before using insertblock. 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>
              
              1 Reply Last reply
              0

              • Login

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