QPaintedTextureImage in Qt3D (Qt 5.8)
QPaintedTextureImage in Qt3D (Qt 5.8)
我想用Qt3D创建一个实体,该实体将自定义图像作为纹理。我遇到了QPaintedTextureImage(链接指向Qt 5.9版本以获取详细信息。这里是 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 是具有 paint 函数的子类化 QPaintedTextureImage:
class TextureImage : public Qt3DRender::QPaintedTextureImage
{
public:
void paint(QPainter* painter);
};
传递给 paint 函数的 QPainter 在 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 中动态加载所需的图像可能更简单。不久前我不得不这样做,并为此在SO上提出了一个问题:
Qt3D动态纹理
相关文章:
- cuda 10.2 in Qt 5.14 ubuntu 18.04
- QPaintedTextureImage in Qt3D (Qt 5.8)
- 没有成员命名 'setRawData' in 'QwtPlotCurve' - 转换 Qt 4.7 为 Qt 5.8
- 没有成员命名 'setResizeMode' in 'QHeaderView' - 转换 Qt 4.7 为 Qt 5.8
- QT c++ "Windows.h no such a file or dictionary in main.ccp"
- UCHAR to Qstring in QT C++
- AES 256 encryption in C++ and Qt 5
- Qt: set columns in treeView
- unique_ptr in Qt project
- QT QTcpServer in thread;如何在退出时关闭侦听服务器
- Qt控制台应用程序"WARNING: QApplication was not created in the main() thread"
- c++ QT and OpenCV. setMouseCallBack in QLabel?
- QSystemTrayIcon in ubuntu, sni-qt
- Qt textChange() in QTextEdit
- Qt + OpenCV in Debian
- Qt QPainter in paintEvent消除了使用paintGL绘制的图形
- const char* to LPCWSTR in Qt
- Qt GUI in a library?
- Qt 中 OpenCV "Can't load OpenGL extension [glBindBuffer] in function IntGetProcAddress"例外
- 分段错误 - OpenCV 和 Qt in Netbeans on Ubuntu