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 QTextDocument color

    Hey there.

    I'm using a QTextDocument to display paragraphes.
    Is there a way to set the "default" text color.

    Thanks.

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

    Default Re: QTextDocument color

    Qt Code:
    1. QString style = "color: rgb(%1,%2,%3);";
    2. mTextDocument->setDefaultStyleSheet(style.arg(mColor.red()).
    3. arg(mColor.green()).
    4. arg(mColor.blue()));
    To copy to clipboard, switch view to plain text mode 

    Tried this with no success.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextDocument color

    Shouldn't it be QString("color: %1;").arg(mColor.name()) ?

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

    Default Re: QTextDocument color

    Should be working with rgb :/.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextDocument color

    What do you do with the document afterwards? Maybe it's enough to set the color in the object (widget?) used as a container for the document?

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

    Default Re: QTextDocument color

    Actually it's not set in a widget,
    I use the TextDocument for HTML strings that are displayed by my view's paint event.
    Last edited by bunjee; 15th April 2008 at 18:04.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextDocument color

    Why don't you add the style definition to the html manually?

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

    Default Re: QTextDocument color

    I was thinking doing so. I was just checking that nobody had a better solution.

    And beside I'm not a big fan of Html parsing .

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextDocument color

    You can also set a QTextFormat for each document block. Setting a pen with a colour of your choice on the painter in the paint event before painting might also help.

  10. #10
    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.

  11. #11
    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.

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.