[Interest] Text editor's line numbers: misalignment

Sensei senseiwa at gmail.com
Wed Aug 22 10:51:20 CEST 2012


On 8/22/12 10:09am, Diego Iastrubni wrote:
> 1) use the same font for the line number as the text editor -
>
> or
>
> 2) set the hight of each line in the panel, as the hight of each line in
> the editor.
>


Actually, that is what the code from Qt does. It wouldn't explain why 
the code works with every font I've tried *except* Monaco.

When you set the font in the constructor, both line numbers and editor 
use the same QFont object. From 
http://qt-project.org/doc/qt-4.8/widgets-codeeditor-codeeditor-cpp.html, 
as I understand, the method lineNumberAreaPaintEvent uses the current 
editor's font and font metrics:


void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
  {
      QPainter painter(lineNumberArea);
      painter.fillRect(event->rect(), Qt::lightGray);


      QTextBlock block = firstVisibleBlock();
      int blockNumber = block.blockNumber();
      int top = (int) 
blockBoundingGeometry(block).translated(contentOffset()).top();
      int bottom = top + (int) blockBoundingRect(block).height();

      while (block.isValid() && top <= event->rect().bottom()) {
          if (block.isVisible() && bottom >= event->rect().top()) {
              QString number = QString::number(blockNumber + 1);
              painter.setPen(Qt::black);
              painter.drawText(0, top, lineNumberArea->width(), 
fontMetrics().height(),
                               Qt::AlignRight, number);
          }

          block = block.next();
          top = bottom;
          bottom = top + (int) blockBoundingRect(block).height();
          ++blockNumber;
      }
  }



Anyway, are you experiencing this behavior or is it just me?




More information about the Interest mailing list