Qt3D 中的 QPaintedTextureImage (Qt 5.8)
QPaintedTextureImage in Qt3D (Qt 5.8)
我想用 Qt3D 创建一个以自定义图像作为纹理的实体。我遇到了 QPaintedTextureImage (link leads to Qt 5.9 version for details. Here ist doc for 5.8),它可以用 QPainter 编写,但我不明白如何。
首先,这就是我想象中实体的样子:
[编辑]:代码已编辑,现在可以使用了!
planeEntity = new Qt3DCore::QEntity(rootEntity);
planeMesh = new Qt3DExtras::QPlaneMesh;
planeMesh->setWidth(2);
planeMesh->setHeight(2);
image = new TextureImage; //see below
image->setSize(QSize(100,100));
painter = new QPainter;
image->paint(painter)
planeMaterial = new Qt3DExtras::QDiffuseMapMaterial;
planeMaterial->diffuse()->addTextureImage(image);
planeEntity->addComponent(planeMesh);
planeEntity->addComponent(planeMaterial);
TextureImage 是具有绘画功能的 QPaintedTextureImage 的子类:
class TextureImage : public Qt3DRender::QPaintedTextureImage
{
public:
void paint(QPainter* painter);
};
传给paint函数的QPainter,如果我只想给planeEntity画一个大圆圈,paint的实现需要做什么?
[编辑] 实现:
void TextureImage::paint(QPainter* painter)
{
//hardcoded values because there was no device()->width/heigth
painter->fillRect(0, 0, 100, 100, QColor(255, 255, 255));
/* Set pen and brush to whatever you want. */
painter->setPen(QPen(QBrush(QColor(255, 0, 255)) ,10));
painter->setBrush(QColor(0, 0, 255));
/*
* Draw a circle (or an ellipse -- the outcome depends very much on
* the aspect ratio of the bounding rectangle amongst other things).
*/
painter->drawEllipse(0, 0, 100, 100);
}
简短的回答是...使用 QPainter
与您通常使用的方式完全相同。
void TextureImage::paint (QPainter* painter)
{
int w = painter->device()->width();
int h = painter->device()->height();
/* Clear to white. */
painter->fillRect(0, 0, w, h, QColor(255, 255, 255));
/* Set pen and brush to whatever you want. */
painter->setPen(QPen(QBrush(QColor(0, 0, 0)) ,10));
painter->setBrush(QColor(0, 0, 255));
/*
* Draw a circle (or an ellipse -- the outcome depends very much on
* the aspect ratio of the bounding rectangle amongst other things).
*/
painter->drawEllipse(0, 0, w, h);
}
但是请注意,您确实不应该直接调用 paint
方法。而是使用 update
这将导致 Qt 安排重绘,初始化 QPainter
并使用指向该画家的指针调用重写的 paint
方法。
在 QML 中动态加载您需要的图像可能会更简单。
不久前我不得不这样做并为此打开了一个问题:
我想用 Qt3D 创建一个以自定义图像作为纹理的实体。我遇到了 QPaintedTextureImage (link leads to Qt 5.9 version for details. Here ist doc for 5.8),它可以用 QPainter 编写,但我不明白如何。 首先,这就是我想象中实体的样子:
[编辑]:代码已编辑,现在可以使用了!
planeEntity = new Qt3DCore::QEntity(rootEntity);
planeMesh = new Qt3DExtras::QPlaneMesh;
planeMesh->setWidth(2);
planeMesh->setHeight(2);
image = new TextureImage; //see below
image->setSize(QSize(100,100));
painter = new QPainter;
image->paint(painter)
planeMaterial = new Qt3DExtras::QDiffuseMapMaterial;
planeMaterial->diffuse()->addTextureImage(image);
planeEntity->addComponent(planeMesh);
planeEntity->addComponent(planeMaterial);
TextureImage 是具有绘画功能的 QPaintedTextureImage 的子类:
class TextureImage : public Qt3DRender::QPaintedTextureImage
{
public:
void paint(QPainter* painter);
};
传给paint函数的QPainter,如果我只想给planeEntity画一个大圆圈,paint的实现需要做什么?
[编辑] 实现:
void TextureImage::paint(QPainter* painter)
{
//hardcoded values because there was no device()->width/heigth
painter->fillRect(0, 0, 100, 100, QColor(255, 255, 255));
/* Set pen and brush to whatever you want. */
painter->setPen(QPen(QBrush(QColor(255, 0, 255)) ,10));
painter->setBrush(QColor(0, 0, 255));
/*
* Draw a circle (or an ellipse -- the outcome depends very much on
* the aspect ratio of the bounding rectangle amongst other things).
*/
painter->drawEllipse(0, 0, 100, 100);
}
简短的回答是...使用 QPainter
与您通常使用的方式完全相同。
void TextureImage::paint (QPainter* painter)
{
int w = painter->device()->width();
int h = painter->device()->height();
/* Clear to white. */
painter->fillRect(0, 0, w, h, QColor(255, 255, 255));
/* Set pen and brush to whatever you want. */
painter->setPen(QPen(QBrush(QColor(0, 0, 0)) ,10));
painter->setBrush(QColor(0, 0, 255));
/*
* Draw a circle (or an ellipse -- the outcome depends very much on
* the aspect ratio of the bounding rectangle amongst other things).
*/
painter->drawEllipse(0, 0, w, h);
}
但是请注意,您确实不应该直接调用 paint
方法。而是使用 update
这将导致 Qt 安排重绘,初始化 QPainter
并使用指向该画家的指针调用重写的 paint
方法。
在 QML 中动态加载您需要的图像可能会更简单。 不久前我不得不这样做并为此打开了一个问题: