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

[REG] QTextDocument mangles file:// URLs used to load images

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P1: Critical P1: Critical
    • None
    • 6.5.3, 6.6.1
    • GUI: Other
    • None
    • All

      In Qt 6, <img src="file:///..."> the file URL is magled by QTextDocument when trying to load the resource. The URL passed to loadResource() is the original URL with the leading file:/ stripped. QUrl.isRelative() returns True for such URLs causing loadResource() to fail on Windows. On Unix it still works, because the leading double slash is equivalent to a leading single slash. On Windows, file:///C:/some/path becomes //c/some/path which fails to load.

       

      A trivial PyQt6 script to reproduce the issue:

      import os
      from PyQt6.QtCore import QUrl
      from PyQt6.QtGui import QTextDocument, QPainter, QImage
      from PyQt6.QtWidgets import QApplication
      
      class D(QTextDocument):
          def loadResource(self, rtype, qurl):
              print('actual URL:', qurl)
              ans = super().loadResource(rtype, qurl)
              if not ans:
                  raise Exception(f'Failed to load: {qurl}')
              return ans
      app = QApplication([])
      d = D()
      url = QUrl.fromLocalFile(os.path.abspath(__file__)).toString()
      d.setHtml(f'<p><img src="{url}">')
      print('expected url:', url)
      img = QImage(100, 100, QImage.Format.Format_ARGB32_Premultiplied)
      p = QPainter(img)
      d.drawContents(p)
      del d
      del app 

      Running this script produces no errors on Unic like OSes. On Windows it fails with the Exception Failed to load URL, because QTextDocument::loadResource() fails for the mangled URL passed to it.

       

      This bug breaks using images with file:// URLs in all widgets that use QTextDocument which include, QTextEdit, QToolTip, QTextBrowser, etc. etc. It is not present Qt 5.

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

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            kovidgoyal Kovid Goyal
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes