[Qt-interest] QTextCursor setForeground changes fixed-width font width?
John McKisson
john.mckisson at gmail.com
Fri Feb 26 17:23:46 CET 2010
I have written an ANSI color parser that displays text to a QTextEdit widget.
I store color attributes for strings and insert them by modifying the
cursor TextCharFormat and calling insertText(text, format).
Abridged code:
QTextCursor cursor = textCursor();
for (Loop over attributed strings) {
QTextCharFormat format; //format =
cursor.charFormat() has the same effect
format.setForeground(ansiForeColor); //just a QColor
cursor.insertText(text, format);
}
I have set my document default font to a fixed width font (using Mac
OS X, Monaco/Andale Mono/Menlo/Courier, etc) and I expect the lines
of text being displayed to line up accordingly. When using
setForeground, the color is being set properly but the spacing looks
like it changes
slightly so the alignment is broken. When I copy/paste the text from
the QTextEdit into another text editing application the spacing is
displayed
properly, so it appears that the rich text is being copied with the
proper formatting.
If I use straight insertText without using setForeground, the text
lines up properly but of course have no color.
I have tried several methods of using setForeground, such as:
QBrush fgBrush;
fgBrush.setColor(ansiForeColor);
format.setForeground(fgBrush);
This does not change the font color at all.
I have verified that the font remains the same as my document default
font before all calls to insertText. Is there something I am missing
about
the use of setForeground? Is there another possibly better way of
inserting colored rich text to a QTextEdit (not HTML)?
Also another interesting note, even after selecting a fixed width font
using a QFontDialog, the call to format.fontFixedPitch() returns
false!
Thanks for any insight.
More information about the Qt-interest-old
mailing list