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

QQuickGradientStop does not use default position when it is not provided

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P2: Important P2: Important
    • None
    • 5.0.0, 5.15.3, 6.10.0 FF, 6.12
    • None
    • ed607c86a (dev), 600cb8d8d (6.10)

      In QQuick gradient, The QQuickGradientStop class does not initialize m_position to 0.0 that the dcoumentation claims to.

      /*!    \qmlproperty real QtQuick::GradientStop::position    \qmlproperty color QtQuick::GradientStop::color
          The position and color properties describe the color used at a given    position in a gradient, as represented by a gradient stop.
          The default position is 0.0; the default color is black.
          \sa Gradient*/
      
      QQuickGradientStop::QQuickGradientStop(QObject *parent)    
      : QObject(parent)
      {
      } 

      While most users do provide the position for each GradientStop, it is possible to forget about it. As a result, an uninitialized value is used, which can cause undefined behavior to CPU/GPU.

      I added a log in where the gradient is used.

          Rectangle {
              ....
              gradient: Gradient {
                  GradientStop {
                      color: xxxxx
                  }
                  GradientStop {
                      position: 1
                      color: xxxxx
                  }
              }
              Component.onCompleted: { console.log("Gradientstop 0 position: ", gradient.stops[0].position); }      
          }
      
      

      and here is the log 

      2025-09-18_16:20:27.83803 qml: Gradientstop 0 position:  0
      2025-09-18_16:20:27.83854 qml: Gradientstop 0 position:  0
      2025-09-18_16:20:29.32487 qml: Gradientstop 0 position:  0
      2025-09-18_16:20:29.32517 qml: Gradientstop 0 position:  0
      2025-09-18_16:20:29.32861 qml: Gradientstop 0 position:  0
      2025-09-18_16:20:29.32976 qml: Gradientstop 0 position:  2.2420775429197073e-43
      2025-09-18_16:20:29.33046 qml: Gradientstop 0 position:  0
      2025-09-18_16:20:29.33066 qml: Gradientstop 0 position:  0
      2025-09-18_16:20:29.33114 qml: Gradientstop 0 position:  0
      2025-09-18_16:20:29.33136 qml: Gradientstop 0 position:  5.75528247281909e-8
      2025-09-18_16:21:33.83072 qml: Gradientstop 0 position:  NaN 

      Clearly, random value is picked up on each time the gradient is created. I have checked that such bug exists on every version of Qt for as long rectangle gradient was available

      Fix:

      QQuickGradientStop::QQuickGradientStop(QObject *parent)    
      : QObject(parent),
        m_position(0.0)
      {
      }  

        For Gerrit Dashboard: QTBUG-140364
        # Subject Branch Project Status CR V

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            petergao Peter Gao
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:

                There is 1 open Gerrit change