Results 1 to 12 of 12

Thread: QTextDocument color

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QTextDocument color

    Unfortunately, that didn't help,

    I'm still trying to figure out how I can set a default color for a QTextDocument without erasing html tags and stuff like that.

    Changing the pen in the paint event has no effect.
    There's got to be some convenient way to do so.

  2. #2
    Join Date
    Jul 2018
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Lightbulb Re: QTextDocument color

    A QTextDocument is never alone. You ought to draw it in a paint function. There, you pass a context parameter. The latter might be just a QAbstractTextDocumentLayout.PaintContext(). It has a palette attribute. There, you set the brush.

    That's how it might look like:
    Qt Code:
    1. class StyledItemDelegate(QStyledItemDelegate):
    2. def paint(
    3. self,
    4. painter: QPainter,
    5. ) -> None:
    6. self.initStyleOption(option, index)
    7. style: QStyle
    8. if option.widget:
    9. style = option.widget.style()
    10. else:
    11. style = QApplication.style()
    12. doc.setHtml(option.text)
    13. option.text = ""
    14. style.drawControl(QStyle.ControlElement.CE_ItemViewItem, option, painter)
    15. ctx: QAbstractTextDocumentLayout.PaintContext = QAbstractTextDocumentLayout.PaintContext()
    16. text_rect: QRect = style.subElementRect(QStyle.SubElement.SE_ItemViewItemText, option)
    17. painter.save()
    18.  
    19. if option.state & QStyle.StateFlag.State_Selected:
    20. ctx.palette.setBrush(QPalette.ColorRole.Text, option.palette.highlightedText())
    21.  
    22. painter.translate(text_rect.topLeft())
    23. painter.setClipRect(option.rect.translated(-text_rect.topLeft()))
    24. painter.translate(0, 0.5 * (option.rect.height() - doc.size().height()))
    25. doc.documentLayout().draw(painter, ctx)
    26. painter.restore()
    To copy to clipboard, switch view to plain text mode 

    Punctual as always, me.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTextDocument color

    You do realize that the post you are replying to is nearly 17 years old, right? And that the Qt document links you have provided are to Qt 4.8, two major versions ago, and that they lead to non-existent 404 pages?

    Edit:
    Actually, it looks like those links might have been automatically inserted by the forum, so never mind.
    Last edited by d_stranz; 20th March 2025 at 00:23.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Using a QFrame as a color selection indicator
    By KShots in forum Qt Tools
    Replies: 8
    Last Post: 14th June 2011, 23:55
  2. Change QPushButton Background Color, Qt4.3
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2009, 07:14
  3. How to change color of a QPushButton?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2008, 13:22
  4. How to fill the grid with the color?
    By merry in forum Qt Programming
    Replies: 3
    Last Post: 18th June 2007, 11:10
  5. QTextDocument - color, font
    By kemp in forum Qt Programming
    Replies: 6
    Last Post: 24th January 2007, 13:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.