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. [Solved] How to change the default color of QTextDocument PlainText
Forum Update on Monday, May 27th 2025

[Solved] How to change the default color of QTextDocument PlainText

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 6.3k 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.
  • M Offline
    M Offline
    messi
    wrote on 29 Oct 2012, 10:51 last edited by
    #1

    Hi everybody,

    I'm trying to find a way to change the default text color of QTextDocument.
    Unfortunately Richtext is not an option for me.
    I have to go for plainText. I would appreciate any tips.

    Thanks for the help.

    Best regards
    messi

    Here is an example.
    I have tried various solutions but with no success.

    1. Attempt:
    Change the blockformat:
    texDocument.firstBlock().blockFormat().setForeground(QBrush(Qt::green,Qt::SolidPattern));

    2. Attempt:
    Change the penn and the brush od the painter:
    painter.setBrush(QBrush(Qt::green,Qt::SolidPattern));
    painter.setPen(QColor(Qt::red));

    //Example

    @#include <QtGui>
    #include <QString>

    const QString text("BlaBla\nWuWu");

    int main(int argv, char *args[])
    {
    QApplication app(argv, args);
    QWidget mw;

    QTextDocument texDocument;
    texDocument.setDefaultFont( QFont("Libaration", 12, -1, true ) );
    texDocument.setPlainText(text);
    
    QPixmap pixmap(texDocument.size().toSize());
    pixmap.fill( Qt::transparent );
    QPainter painter( &pixmap );
    
    texDocument.drawContents(&painter, pixmap.rect());
    
    QPushButton button(&mw);
    button.setIcon(QIcon(pixmap));
    button.setIconSize(pixmap.size());
    
    mw.resize(100, 70);   
    mw.show();
    
    return app.exec(&#41;;
    

    }@

    1 Reply Last reply
    0
    • M Offline
      M Offline
      messi
      wrote on 29 Oct 2012, 12:27 last edited by
      #2

      I found a solution. The problem is, that QTextDocumentLayout::draw takes a parameter QAbstractTextDocumentLayout::PaintContext ctx. This one is defined inside QTextDocument.

      Now I wrote a new drawContents function which takes an additional parameter QTextDocument.
      The example from above works with this additional function.

      @void drawContents(QTextDocument *td, QPainter *p, const QRectF &rect)
      {
      p->save();
      QAbstractTextDocumentLayout::PaintContext ctx;

      ctx.palette.setColor(QPalette::Text, p->pen().color());
      
      if (rect.isValid()) 
      {
          p->setClipRect(rect);
          ctx.clip = rect;
      }
      
      td->documentLayout()->draw(p, ctx);
      p->restore();
      

      }@

      1 Reply Last reply
      0

      1/2

      29 Oct 2012, 10:51

      • Login

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