Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Unable to assign GenericDataModel to GenericDataModel

    QML and Qt Quick
    qml c++ model view modelview
    1
    3
    1001
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      LScott last edited by

      I'm developing a large Qt-based framework, so I'll try to give watered-down minimal examples where possible.
      I have a plugin system for defining new data models for viewing though custom QML view components.

      These plugins are dynamically loaded using QPluginLoader, as with the Plug 'n' Paint example. In my plugin interface, I can call a function that supplies me with a pointer to a newly allocated base class:

      GenericDataModel *mdl = mod->CreateModel("frt");
      

      where mod is a pointer to the loaded plugin interface. GenericDataModel is a derived class of QAbstractItemModel, already confirmed to work with other models. GenericDataModel is also registered as a type with QML:

      qmlRegisterType<GenericDataModel>("plugin.genericmodel", 1,  0, "GenericDataModel");
      

      This works fine, and I then load the data into the model without any problems. I confirm with the debugger that the data is there and laid out correctly. I then register the model into QML like so:

      QQmlApplicationEngine engine;
      QQmlContext *ctxt = engine.rootContext();
      ctxt->setContextProperty("MDLFileTest", QVariant::fromValue(mdl));
      

      I then apply this to a custom QML view, CustomList.qml:

      import QtQuick 2.7
      import QtQml.Models 2.2
      import QtQuick.Controls 2.2
      import plugin.genericmodel 1.0
      
      ListView {
          id: listView
          property GenericDataModel dataModel
      
          model: dataModel
          delegate { ... }
      }
      

      Which I then apply to a page:

      CustomList {
          id: customList
          dataModel: MDLFileTest
      }
      

      When loading this from a plugin (i.e. the plugin defines a class that extends GenericDataModel, and allocates one and returns it as a GenericDataModel* to the main application), I get the QML error at runtime:

      Unable to assign GenericDataModel to GenericDataModel

      At this point, as far as the QML layer is concerned, I've assigned a GenericDataModel for a property expecting a GenericDataModel. However, when I do exactly the same thing, but move the header and source file for the derived class into the main application, so that it is compiled in, it works perfectly...

      Can somebody explain this error to me? I've seen it before with two different types that cannot be casted, but this is an error trying to assign type X to type X! Quite strange. I have played around with the Q_OBJECT macro and gotten it to read:

      Unable to assign FRTFileModel to GenericDataModel

      The strange thing is, FRTFileModel is a direct descendant of GenericDataModel!

      I tried to keep this question as minimal as possible, but if anyone wants me to try to create a minimal example project I can give it a go as well.
      Thanks!

      1 Reply Last reply Reply Quote 0
      • L
        LScott last edited by

        I feel like I have somehow created a situation where there are 2 class types with the name "GenericDataModel"... The GenericDataModel class is declared and defined inside a seperate library that is statically linked to both the application and the plugin. Calling

        mdl->inherits("GenericDataModel");
        

        returns true, yet calling

        qobject_cast<GenericDataModel*>(mdl);
        

        returns null.

        Am I correct in thinking that this means that I have 2 separate class types that have the same name in the Qt meta object system? Obviously this is not what I am intending... How can I fix this? Both the plugin and the application include the same genericdatamodel.h from a static library, and both link to the same static library? Is this not the way that I should be doing this?

        Please give your input, thanks.

        1 Reply Last reply Reply Quote 0
        • L
          LScott last edited by

          Interestingly, if I make the plugin static, it works fine. Only when the plugin is dynamic does this error happen... Very strange.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post