Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Unable to assign GenericDataModel to GenericDataModel
QtWS25 Last Chance

Unable to assign GenericDataModel to GenericDataModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlc++modelviewmodelview
3 Posts 1 Posters 1.2k Views
  • 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 Offline
    L Offline
    LScott
    wrote on 9 Jun 2017, 21:40 last edited by
    #1

    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
    0
    • L Offline
      L Offline
      LScott
      wrote on 12 Jun 2017, 17:49 last edited by
      #2

      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
      0
      • L Offline
        L Offline
        LScott
        wrote on 12 Jun 2017, 18:03 last edited by
        #3

        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
        0

        1/3

        9 Jun 2017, 21:40

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved