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. QSortFilterProxyModel sub-class registration and instantiation issue.
Forum Updated to NodeBB v4.3 + New Features

QSortFilterProxyModel sub-class registration and instantiation issue.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 288 Views 1 Watching
  • 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.
  • SiN_BizkitS Offline
    SiN_BizkitS Offline
    SiN_Bizkit
    wrote on last edited by
    #1

    Hi everyone!
    I have a problem with instantiating of an QSortFilterProxyModel derived class instance directly in QML.
    For e.g. I defined a class

    class FilterModel: public QSortFilterProxyModel {
    	Q_OBJECT
    public:
    	bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
    };
    

    Then I register the class:

    static void registerQmlTypes()
    {
    	qmlRegisterType<FilterModel>("com.mycompany.app", 1, 0, "FilterModel");
    }
    Q_COREAPP_STARTUP_FUNCTION(registerQmlTy
    

    And use it:

    import QtQuick 2.12
    import QtQuick.Controls 2.12
    import com.mycompany.app 1.0 as App
    ListView {
    	model: App.FilterModel {
    		sourceModel: App.someModel
    	}
        delegate: Item { /** */ }
    }
    

    The problem is that it's sometimes can't load the qml file on process start and terminates. Error message:

    Cannot assign object of type "App.FilterModel" to property of type "QAbstractItemModel*" as the former is neither the same as the latter nor a sub-class of it.
    

    And this issue isn't persistent. Sometimes it occurs and sometimes not.
    Anybody have any idea how can I fix it? I know that I can instantiate the model in C++ part and then pass to the QML, but I think it is an awkward solution.

    1 Reply Last reply
    0
    • SiN_BizkitS Offline
      SiN_BizkitS Offline
      SiN_Bizkit
      wrote on last edited by
      #2

      Maybe it will help someone. This code fixed the issue:

      #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
      	qmlRegisterAnonymousType<QAbstractItemModel>(uri, 1);
      #else
      	qmlRegisterType<QAbstractItemModel>();
      #endif
      
      1 Reply Last reply
      0

      • Login

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