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

Blurry graphics when using QGraphicsItem due to incorrect devicePixelRatio setting on cached pixmap

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4: Low
    • None
    • 5.15.0
    • Widgets: GraphicsView
    • None
    • Windows 10, 2K screen
    • Windows

    Description

      1. Problem Screenshot:

      See attachments "problem screenshot".

      2. Steps to Reproduce:

      a. Device setup condition:

      • Use a Windows device
      • In display settings, set system scaling to a non-integer value (e.g. 125%)
        If you cannot find the scaling setting, refer to the attached documents screenshot.

      b. Code condition:

      • Add the following code line
        QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        
      • Set Cache Mode in QGraphicsItem
        MyGraphicsItem::MyGraphicsItem(QGraphicsItem* parent)
            : QGraphicsItem(parent)
        {
            // or ItemCoordinateCache, the specific cache mode is not important
            setCacheMode(DeviceCoordinateCache);
        }
        

      3. Source Code Analysis:

      In "/qtbase/src/widgets/graphicsview/qgraphicsscene.cpp", I found this drawItemHelper:

      void QGraphicsScenePrivate::drawItemHelper(QGraphicsItem *item, QPainter *painter,
                                                 const QStyleOptionGraphicsItem *option, QWidget *widget,
                                                 bool painterStateProtection)
      {
      // line 4423
      const qreal devicePixelRatio = painter->device()->devicePixelRatio();
      

      In QGraphicsScenePrivate::drawItemHelper, devicePixelRatio() is used instead of devicePixelRatioF().

      devicePixelRatio() returns an integer by rounding down the ratio(1.25->1). devicePixelRatioF() returns a float preserving decimals.
      This makes me wonder:

      • For high-dpi displays with fractional scaling, is the integer ratio from devicePixelRatio() accurate enough?
      • Should devicePixelRatioF() be used here to get the precise ratio with decimals?
      • Or is there something else I need to set in my application code to properly handle high-dpi fractional scaling?

      4. In conclusion

      I would be grateful if Qt developers could validate the root cause and provide guidance.

      To aid analysis, I have attached a demo project and screenshots demonstrating the problem. Please let me know if any other details are needed.

      Attachments

        1. problem screenshot.jpeg
          problem screenshot.jpeg
          20 kB
        2. Display Setting.jpeg
          Display Setting.jpeg
          320 kB
        3. CacheModeTest.zip
          7 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bibr Andreas Aardal Hanssen
            levi lal Jiahao
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes