Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

JB_NodeModel has no default constructor - Why?



  • I'm trying to register a C++ JB_NodeModel in the Qml Engine. JB_NodeModel inherits QAbstractItemModel.
    I get this error:

    "call to implicitly-deleted default constructor of QQmlElement<JB_NodeModel> - default constructor of QQmlElement<JB_NodeModel> is implicitly deleted because base class JB_NodeModel has no default constructor."

    But I can't see a problem with the default constructor.
    Any help would be greatly appreciated.

    I've used initialiser lists but doesn't seem to help.

    Here is a cut down version of JB_NodeModel.h :

    class SP3CORESHARED_EXPORT JB_NodeModel : public QAbstractItemModel
    {
        Q_OBJECT
    public:
     explicit JB_NodeModel(QObject *parent = nullptr);
    
    ~JB_NodeModel();
    
    protected:
        JB_Node* rootNode;
        JB_Node* actingRootNode;
        bool isValidURL(QString fieldName, JB_Node* node) const;
    
        QVector<QString> columnHeadingsV;    
        QHash<int, QVector<QString>> mapped_dbFieldNamesH;    
        QList<QString> unEditableDBFieldNamesList;
        QList<QString> editableCheckBoxDBFieldNamesList; 
    
    private:
        JB_DatabaseManager& mDB;
        int jobID;
        int numOfLevels;
        QSharedPointer<JB_NodeModelHelpProt> nodeModelHelper;
        QVector<QSharedPointer<QHash<QString, JB_Node*>>> nodesHV;     
        QVector<JB_Node*> alteredNodesV;
        bool isTreeModel;
        bool canEmitDataChanged;
     
     };
    

    Here is JB_NodeModel.cpp constructor source:

    JB_NodeModel::JB_NodeModel(QObject *parent)
    : QAbstractItemModel(parent),
      rootNode(nullptr),
      actingRootNode(nullptr),
      mDB(JB_DatabaseManager::instance()),
      jobID(aJobID),
      numOfLevels(1),
      nodeModelHelper(nullptr),
      isTreeModel(true),
      canEmitDataChanged(true),
      dragDropHelper(false)
    {
        QSharedPointer<JB_NodeModelHelpProt> aNodeModelHelper(new JB_NodeModHelp_TreeGrpPers());
        nodeModelHelper = aNodeModelHelper;
    
        canEmitDataChanged = true;
        dragDropHelper = false;
    
        Q_ASSERT(!nodeModelHelper.isNull());
    
        alteredNodesV.clear();
    
        rootNode = nullptr;
        actingRootNode = nullptr;
    

    etc. - please note I have implemented the virtual functions such as index and data etc but a lot of code so haven't included it here

    And here is registering in QML engine:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    
    #include "A_Models/JB_NodeModel.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        qmlRegisterType<JB_NodeModel>("JB_NodeModel", 1, 0, "JB_NodeModel");
    
        QQmlApplicationEngine engine;
    
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    And here is the relevant part of qqmlprivate.h surrounding QQmlElement:

    class QJSEngine;
    class QQmlEngine;
    class QQmlCustomParser;
    namespace QQmlPrivate
    {
        void Q_QML_EXPORT qdeclarativeelement_destructor(QObject *);
        template<typename T>
        class QQmlElement final : public T
    {
    public:
        ~QQmlElement() override {
            QQmlPrivate::qdeclarativeelement_destructor(this);
        }
        static void operator delete(void *ptr) {
            // We allocate memory from this class in QQmlType::create
            // along with some additional memory.
            // So we override the operator delete in order to avoid the
            // sized operator delete to be called with a different size than
            // the size that was allocated.
            ::operator delete (ptr);
        }
        static void operator delete(void *, void *) {
            // Deliberately empty placement delete operator.
            // Silences MSVC warning C4291: no matching operator delete found
        }
    };
    
    
    template<typename T>
    

    // Error occurs on next line

    void createInto(void *memory) { new (memory) QQmlElement<T>; }
    
    template<typename T>
    QObject *createParent(QObject *p) { return new T(p); }

  • Qt Champions 2017

    How are you creating the object of JB_NodeModel ? I suspect some issue with member variable you have declared & initialised them. May be you can comment all those member variable initialisation & try creating the object.



  • @jeffB100 said in JB_NodeModel has no default constructor - Why?:

    qmlRegisterType<JB_NodeModel>("JB_NodeModel", 1, 0, "JB_NodeModel");

    Can you try to change 1st parameter to something another, maybe "MyModels"?


Log in to reply