How to size the texture to occupy only a portion of a QQuickItem UI

229 views Asked by At

I have overriden updatePaintNode in the following way to draw an OpenGL texture on a QQuickItem derived class called MyQQuickItem here.

QSGNode *MyQQuickItem::updatePaintNode(QSGNode * oldNode, QQuickItem::UpdatePaintNodeData * /*updatePaintNodeData*/) 
{
  QSGSimpleTextureNode * textureNode = static_cast<QSGSimpleTextureNode *>(oldNode);

  if (!textureNode) {
    textureNode = new QSGSimpleTextureNode();
  }

  QSize size(800, 800);
  // myTextureId is a GLuint here
  textureNode.reset(window()->createTextureFromId(myTextureId, size));
  textureNode->setTexture(my_texture);
  textureNode->markDirty(QSGBasicGeometryNode::DirtyMaterial);

  QSizeF myiewport = boundingRect().size();

  qreal xOffset = 0;
  qreal yOffset = 10;

  textureNode->setRect(xOffset, yOffset, myViewport.width(), myViewport.height());
  return textureNode;
}

This renders the texture content well but covers the whole of my MyQQuickItem UI.

How can reduce the bottom margin of the texture to say fit 80% of the height of MyQQuickItem. I want to render the texture to a portion of MyQQuickItem & leave the rest blank or black? Is that possible within updatePaintNode.

Note that the texture size is not the UI window size here. My texture size is 800 by 800. Whereas the UI window size is different and depends on the screen.

1

There are 1 answers

0
TheWaterProgrammer On

I found the answer to this:

Changing myViewport.height() gives the required end in Y direction one wishes to set. Similarly, changing myViewport.width() gives the required end in X direction one wishes to set.

4 parameters in TextureNode's setRect can stretch & fit the texture in the way one wishes within a portion of the QQuickItem.