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

QPainter::drawText(QRectF, QString) has inconsistent behavior when QRectF is null

    XMLWordPrintable

Details

    • c7df8dda8 (dev), 051764ddf (6.8)

    Description

      When calling QPainter::drawText(QRectF, QString) with an invalid rect, e.g.

      painter->drawText(QRectF(), "foo")
      

      then nothing will be drawn unless the painter is rendering to a QSvgGenerator, in which case the text will be displayed at (0, 0). Here's a PySide6 script that demonstrates the issue, but we've experienced this with C++ code as well:

      from PySide6 import QtWidgets, QtGui, QtCore, QtSvg
      from PySide6.QtCore import Qt
      
      app = QtWidgets.QApplication([])
      widget = QtWidgets.QWidget()
      layout = QtWidgets.QHBoxLayout(widget)
      widget.setLayout(layout)
      for test in ['invalid_SVG', 'valid_SVG', 'invalid_PXM', 'valid_PXM']:
          if 'invalid' in test:
              rect = QtCore.QRectF()  # invalid rect
          else:
              rect = QtCore.QRectF(25, 25, 75, 75)  # valid rect
          pixmap = QtGui.QPixmap(100, 100)
          painter = QtGui.QPainter()
          if 'SVG' in test:
              svg_gen = QtSvg.QSvgGenerator()
              buffer = QtCore.QBuffer()
              svg_gen.setOutputDevice(buffer)
              svg_gen.setSize(QtCore.QSize(100, 100))
              painter.begin(svg_gen)
          else:
              painter.begin(pixmap)
          painter.fillRect(QtCore.QRect(0, 0, 100, 100), Qt.white)
          painter.setPen(Qt.black)
          painter.drawText(rect, test)
          painter.end()
          if 'SVG' in test:
              pixmap.loadFromData(buffer.data(), "SVG")
          label = QtWidgets.QLabel(widget)
          layout.addWidget(label)
          label.setPixmap(pixmap)
      widget.show()
      app.exec()
      

      This script generates

      As you can see, "invalid_SVG" is rendered to the upper-left corner of the first image, but nothing is rendered to the third image (where the text is rendered to an invalid QRectF in a pixmap).

      Attachments

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

        Activity

          People

            vgt Eirik Aavitsland
            kevkeating Kevin Keating
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes