Error while qmlRegisterType with multiple inheritance class
-
I have a class called View which inherits from QQuickItem.
And now I create class Box which inherits QQuickPaintedItem and View.class Box : public QQuickPaintedItem, public View { // Q_OBJECT <- this also make errors so I removed it. .... }
The problem is when I register this class with qmlRegisterType, there are some errors.
qmlRegisterType<Box>(uri, 0, 1, "Box");
First is
/usr/include/qt/QtQml/qqmlprivate.h:71:49: error: reference to ‘Box::staticMetaObject’ is ambiguous 71 | const char *className = T::staticMetaObject.className(); \ | ~~~~~~~~~~~~~~~~~^~~~~~~~~
And second is
/usr/include/qt/QtCore/qmetatype.h:1529:40: error: ‘QObject’ is an ambiguous base of ‘Box’ 1529 | enum { Value = sizeof(checkType(static_cast<T*>(nullptr))) == sizeof(yes_type) }; | ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
And last one
/usr/include/qt/QtQml/qqml.h:349:51: error: reference to ‘Box::staticMetaObject’ is ambiguous 349 | uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject, | ^~~~~~~~~~~~~~~~~~~~
Is it impossible to register QML C++ extension that multiple inheritance class?
-
Hi,
@Yujeonja said in Error while qmlRegisterType with multiple inheritance class:
class Box : public QQuickPaintedItem, public View
Any chances that View also inherits QObject like QQuickPaintedItem ?
-
You can't have double inheritance from QObject based class. That is the current issue you are hitting.
Does you view class really need to be QObject based ?
Do you really need to inherit from it rather than use it (i.e. composition) ? -
I discard QQuickPaintedItem and using updatePaintNode with QImage and QSGTexture.
The idea from https://stackoverflow.com/a/58597344/5902108Below is my code.
QSGNode* Box::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *) { QImage canvas(View::width(), View::height(), QImage::Format_ARGB32); QBrush brush; canvas.fill(QColor("transparent")); QPainter painter(&canvas); brush.setStyle(Qt::SolidPattern); brush.setColor(this->color()); painter.setPen(Qt::NoPen); painter.setBrush(brush); painter.drawRect(0, 0, View::width(), View::height()); QSGTexture *texture = this->window()->createTextureFromImage(canvas); QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode*>(oldNode); if (!node) { node = new QSGSimpleTextureNode(); } node->setRect(0, 0, View::width(), View::height()); node->setTexture(texture); return node; }