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

QTextLayout doesn't draw Cursor when background not drawn

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.9.1
    • GUI: Painting
    • None
    • Linux/X11

    Description

      I Have a QGraphicsWidget which draws some text on a QGraphicsScene. In the Paint Method I'm using a QTextDocument and QTextLayout to format an draw the text. The strange thing that I've noticed is that the Cursor (QTextLayout::drawCursor) is not drawn when there is no background painted. As soon as I draw a background (with the same Painter), the cursor is drawn, without the background, no cursor visible.

      void AnnotationText::paint(QPainter *painter, const QStyleOptionGraphicsItem *style, QWidget *widget)
      {
         // Paint border
         AbstractAnnotationRect::paint(painter, style, widget);
      
         auto textArea = mRect->normalized();
         if (mIsInEditMode) {
            setupEditModeOutlinePen();
            painter->setPen(mEditModeOutlinePen);
            painter->drawRect(textArea);
         }
      
         // Paint text
         painter->setPen(properties()->textColor());
         auto margin = properties()->width();
      
         painter->setClipRect(boundingRect().adjusted(margin, margin, -margin, -margin));
      
         QFontMetrics fontMetrics(textProperties()->font());
         auto boxHeight = 0;
      
         QTextDocument document(mText);
         for (auto block = document.begin(); block != document.end(); block = block.next()) {
            auto blockPosition = block.position();
            auto blockLength = block.length();
            QTextLayout textLayout(block);
            textLayout.setFont(textProperties()->font());
            auto blockHeight = 0;
            textLayout.setCacheEnabled(true);
      
            textLayout.beginLayout();
            while (true) {
               auto line = textLayout.createLine();
               if (!line.isValid()) {
                  break;
               }
      
               line.setLineWidth(textArea.width() - margin * 2);
               blockHeight += fontMetrics.leading();
               line.setPosition(textArea.adjusted(margin, margin, 0, 0).topLeft());
               blockHeight += line.height();
            }
            textLayout.endLayout();
      
            textLayout.draw(painter, QPoint(0, boxHeight));
      
            painter->setBrush(Qt::NoBrush);
            painter->drawRect(textArea);
      
            if (mTextCursor.isVisible() && (mTextCursor.position() >= blockPosition && mTextCursor.position() < blockPosition + blockLength)) {
               textLayout.drawCursor(painter, QPointF(0, boxHeight), mTextCursor.position() - blockPosition, 1);
            }
            boxHeight += blockHeight;
         }
      }
      

      Same code build via CI with Qt Verion 5.6.1 works fine, on my PC build as AppImage and on another machine which runs on Windows.  

      Attachments

        Issue Links

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

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              dporobic Damir Porobic
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes