Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does the transparent color on a QQuickItem not have the same behavior as that of a Rectangle?
Forum Updated to NodeBB v4.3 + New Features

Why does the transparent color on a QQuickItem not have the same behavior as that of a Rectangle?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 800 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Babiole
    wrote on last edited by Babiole
    #1

    Hello, i have transparency issue on my QQuickItem, it's color doesn't have the same behaviour of a Rectangle's color for example.
    I'm on Qt 5.9.1

    This is my CustomShape :

    QSGNode *RoundedRectangle::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data)
    {
        const auto r = m_fillColor.red();
        const auto g = m_fillColor.green();
        const auto b = m_fillColor.blue();
        const auto alpha = m_fillColor.alpha();
         if (!oldNode) {
    		node = new QSGGeometryNode;
    		geometry = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), nbVertice);
                    geometry->setDrawingMode(QSGGeometry::DrawTriangleStrip);
                    const auto material = new QSGVertexColorMaterial;
                    material->setFlag(QSGMaterial::Blending, true);
                    node->setGeometry(geometry);
                    node->setMaterial(material);
                    node->setFlag(QSGNode::OwnsMaterial);
    	}
    	else {
    		node = static_cast<QSGGeometryNode *>(oldNode);
    		geometry = node->geometry();
    		geometry->allocate(nbVertice);
    	}
    	auto vertices = geometry->vertexDataAsColoredPoint2D();
    //Just an exemple of setting a vertice
    	vertices[++idx].set(posX, posY, r, g, b, alpha);
            ...
           return node;
    }
    

    This is my qml file :

    RoundedRectangle {
            id:test
            anchors.top: canvas.top
            anchors.topMargin: margin
            anchors.left: canvas.left
            anchors.leftMargin: margin
            anchors.right: canvas.right
            anchors.rightMargin: margin
            height: 20
            width: 200
            borderWidth: 1
            radius: 5
            borderColor: "#C8E6C9"
            fillColor: "#501B5E20"
            antialiasing: true
        }
    
        Rectangle {
            anchors.top: test.bottom
            anchors.topMargin: margin
            anchors.left: canvas.left
            anchors.leftMargin: margin
            anchors.right: canvas.right
            anchors.rightMargin: margin
            height: 20
            width: 200
            radius: 5
            color: "#501B5E20"
        }
    

    And this is the result :
    alt text
    Same color but different result (fillcolor and color on qml)
    My item is the first rectangle of the picture. The basic rectangle is just below.

    What I did wrong?

    B 1 Reply Last reply
    0
    • B Babiole

      Hello, i have transparency issue on my QQuickItem, it's color doesn't have the same behaviour of a Rectangle's color for example.
      I'm on Qt 5.9.1

      This is my CustomShape :

      QSGNode *RoundedRectangle::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data)
      {
          const auto r = m_fillColor.red();
          const auto g = m_fillColor.green();
          const auto b = m_fillColor.blue();
          const auto alpha = m_fillColor.alpha();
           if (!oldNode) {
      		node = new QSGGeometryNode;
      		geometry = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), nbVertice);
                      geometry->setDrawingMode(QSGGeometry::DrawTriangleStrip);
                      const auto material = new QSGVertexColorMaterial;
                      material->setFlag(QSGMaterial::Blending, true);
                      node->setGeometry(geometry);
                      node->setMaterial(material);
                      node->setFlag(QSGNode::OwnsMaterial);
      	}
      	else {
      		node = static_cast<QSGGeometryNode *>(oldNode);
      		geometry = node->geometry();
      		geometry->allocate(nbVertice);
      	}
      	auto vertices = geometry->vertexDataAsColoredPoint2D();
      //Just an exemple of setting a vertice
      	vertices[++idx].set(posX, posY, r, g, b, alpha);
              ...
             return node;
      }
      

      This is my qml file :

      RoundedRectangle {
              id:test
              anchors.top: canvas.top
              anchors.topMargin: margin
              anchors.left: canvas.left
              anchors.leftMargin: margin
              anchors.right: canvas.right
              anchors.rightMargin: margin
              height: 20
              width: 200
              borderWidth: 1
              radius: 5
              borderColor: "#C8E6C9"
              fillColor: "#501B5E20"
              antialiasing: true
          }
      
          Rectangle {
              anchors.top: test.bottom
              anchors.topMargin: margin
              anchors.left: canvas.left
              anchors.leftMargin: margin
              anchors.right: canvas.right
              anchors.rightMargin: margin
              height: 20
              width: 200
              radius: 5
              color: "#501B5E20"
          }
      

      And this is the result :
      alt text
      Same color but different result (fillcolor and color on qml)
      My item is the first rectangle of the picture. The basic rectangle is just below.

      What I did wrong?

      B Offline
      B Offline
      Babiole
      wrote on last edited by Babiole
      #2

      @Babiole
      I found the answer, this is because QSGVertexColorMaterial interpret color as premultiplied alpha. So you have to multiply your base color by your alpha percentage.

      const auto r = m_fillColor.red() * m_fillColor.alphaF();
      const auto g = m_fillColor.green() * m_fillColor.alphaF();
      const auto b = m_fillColor.blue() * m_fillColor.alphaF();
      const auto alpha = m_fillColor.alpha();
      
      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved