0

I want to use a nested QObject structure in QML, because the nested object should receive necessary parameters from the first-level object. The structure looks as follows.

The solution builds, but when I try to run it, I received the error:

QMetaProperty::read: Unable to handle unregistered datatype 'Projector' for property 'Backend::projector'

main.cpp

#include "backend.h"

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

int main(int argc, char** argv) {
  QApplication app(argc, argv);
  QQmlApplicationEngine engine;

  qmlRegisterType<Backend>("com.company.backend", 1, 0, "Backend");

  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

  return app.exec();
}

main.qml

import QtQuick 2.11
import QtQuick.Window 2.0
import QtQuick.Controls 2.4
import com.company.backend 1.0


Window
{

....

ComboBox {
        id: projector
        currentIndex: 2
        model: backend.projector.list
        width: 180
        anchors.left: parent.left
        anchors.top: projectorListLabel.bottom
        anchors.leftMargin: 15
        anchors.topMargin: 5
    }

...

}

backend.h

...

#include "projector.h"

class Backend : public QObject {
  Q_OBJECT
  Q_PROPERTY(Projector projector READ getProjector NOTIFY projectorChanged)

public:
  Projector* getProjector() const;

signals:
  void projectorChanged();

backend.cpp

...

#include "backend.h"

Backend::Backend(QObject* parent) {

  _projector = new Projector();
  emit(projectorChanged());
}

Projector* Backend::getProjector() const {
  return _projector;
}

projector.h

...

class Projector : public QObject {
  Q_OBJECT

  Q_PROPERTY(QStringList list READ getList WRITE setList NOTIFY listChanged)

 public:
  explicit Projector(QObject* parent = nullptr);

 QStringList getList() const;

 public slots:
  void setList(QStringList list);

 signals:
  void listChanged();

 private:
  QStringList _list;
};

projector.cpp

...

#include "projector.h"

#include <iostream>

Projector::Projector(QObject* parent) {}

QStringList Projector::getList() const {
  return _list;
}

void Projector::setList(QStringList list) {
  std::cout << "Updating list" << std::endl;
  _list = list;
  listChanged();
}

I have read some article about registering the meta-type and using macros, but I have not found any source that explains this thoroughly.

1
  • 1
    You also need to register Projector with qmlRegisterType.
    – iam_peter
    Sep 1, 2022 at 15:20

1 Answer 1

0

QT needs to know the definitions of the types you'll be using for signals/slots etc.

The type can be declared at compilation time using the Q_DECLARE_METATYPE macro

However, you also probably need to register it at runtime to enable the creation of new instances. This is done through the qRegisterMetatype() method.

See this link for the full documentation which explains this nicely

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.