Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-8791

Changing text color using QTextCursor causes fixed width fonts to become misaligned

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 4.7.0
    • 4.6.2
    • None
    • Have only tested on OS X 10.6, Qt 4.6.2 (Cocoa), Windows/Linux not available to me
    • ef9e7168a8ed3e8149204cf6c604a38db35245c0

    Description

      The code below should display two lines of text with perfect vertical alignment, however the colored line is slightly longer.
      It appears each time a color is changed the next character is spaced by an additional pixel.

      #include <QtGui>
      #include <QtDebug>
      
      int main(int argv, char **args) {
          QApplication app(argv, args);
      	QTextEdit textEdit;
      	QFont fixedWidthFont("Courier");
      	textEdit.document()->setDefaultFont(fixedWidthFont);
      	if (!textEdit.document()->defaultFont().fixedPitch())
      		qDebug() << textEdit.document()->defaultFont() << "not fixed pitch?!";
      
      	textEdit.show();
      
      	QTextCursor cursor = textEdit.textCursor();
      
      	cursor.insertText("...RGB...");
      	cursor.insertBlock();
      	QTextCharFormat format = cursor.charFormat();
      	format.setForeground(QColor(0, 0, 0));
      	cursor.insertText("...", format);
      	format.setForeground(QColor(255, 0, 0));
      	cursor.insertText("R", format);
      	format.setForeground(QColor(0, 255, 0));
      	cursor.insertText("G", format);
      	format.setForeground(QColor(0, 0, 255));
      	cursor.insertText("B", format);
      	format.setForeground(QColor(0, 0, 0));
      	cursor.insertText("...", format);
      
          return app.exec();
      }
      

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              esabraha Eskil Abrahamsen Blomfeldt
              jmckisson John McKisson
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes