Results 1 to 7 of 7

Thread: QTextDocument background colour

  1. #1
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QTextDocument background colour

    Hello everyone,

    I am writing a syntax highlighter with QPlainTextEdit, QSyntaxHighlighter and QTextDocument. I want to set the background colour of the editor. I successfully managed to set the background colour of the QPlainTextEdit, and I can set the background colour of individual text blocks with syntax highlighting. However, QTextDocument seems to render a little margin around the text. It renders a white border around the document. Also setting the background colour explicitly every time when highlighting seems unnecessary, it would be better to set the background colour of the entire document. I have tried to use setDefaultStyleSheet but that doesn't work. So how do I set the background colour of a QTextDocument?

    Qt 4.6, Windows at the moment but should also apply to other platforms


    Thanks in advance,

    Ruud

  2. #2
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument background colour

    This needs to be done in a class which derives QTextEdit or QPlainTextEdit. Do this each time the cursor moves:

    Qt Code:
    1. format.setBackground(QColor(0xc0,0xff,0xc0,0x80));
    2.  
    3. QTextEdit::ExtraSelection selection;
    4. selection.format.setProperty(QTextFormat::FullWidthSelection, true);
    5. selection.format = format;
    6. selection.cursor = textCursor();
    7. selection.cursor.clearSelection();
    8.  
    9. QList<QTextEdit::ExtraSelection> selections;
    10. selections.append( selection );
    11.  
    12. setExtraSelections(selections);
    To copy to clipboard, switch view to plain text mode 

    The trick is QTextEdit::setExtraSelections() . Download the code of QtCreator for some more hidden gems.

  3. #3
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument background colour

    Thanks for the reply! Unfortunately it didn't work. I did find the solution myself after trying some more. The solution was quite simple actually. Instead of setting the default stylesheet for the document, setting the stylesheet of the QPlainTextEdit works:

    Qt Code:
    1. textEdit->setStyleSheet("background-color: "+colour.name()+";");
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextDocument background colour

    You could also try to set a proper palette (QPalette::Base). Which is normally a lighter way compared to using style sheets.

  5. #5
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument background colour

    I did already set the palette, but that changed only the background colour of the area where there is no text (everything below the last line). The background of the text did not change.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextDocument background colour

    Hm, a quick test in designer works for me. But if you are fine with the style sheets, no reason to dig deeper.

  7. #7
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocument background colour

    It's probably the combination of a QPlainTextEdit with a QSyntaxHightlighter that causes the problem, but you are right, no need to search any further.

Similar Threads

  1. fade from one colour to another
    By panduro in forum Newbie
    Replies: 2
    Last Post: 18th June 2008, 13:30
  2. QTextDocument and background-images
    By pfusterschmied in forum Newbie
    Replies: 0
    Last Post: 1st June 2007, 19:26
  3. Dynamically changing QLabel background colour
    By T4ng10r in forum Qt Programming
    Replies: 19
    Last Post: 19th April 2007, 12:47
  4. Column Colour
    By sumsin in forum Qt Programming
    Replies: 8
    Last Post: 15th June 2006, 10:38
  5. background colour
    By kw in forum Qt Programming
    Replies: 6
    Last Post: 11th April 2006, 00:44

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.