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. Custom type registered in QML and QVariant
Forum Updated to NodeBB v4.3 + New Features

Custom type registered in QML and QVariant

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 1.2k 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.
  • Pieter CardoenP Offline
    Pieter CardoenP Offline
    Pieter Cardoen
    wrote on last edited by
    #1

    Dear

    In Qt/QML, it is possible to define custom types from C++ and use them in QML by using:
    qmlRegisterType<CustomType>("ImportName",1,0,"TypeName");

    Qt also has support for the QVariant type which acts as an union for common datatypes. A custom type can be used with QVariant by using:
    Q_DECLARE_METATYPE(CustomType);

    Recently, I was doing implementations with listviews and treeviews where the model inherits from QAbstractItemModel and where this function is provided:
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0

    I would like to use a custom type for visualizing an item of my ListView and it would be great if the data function could return a QVariant which can be converted to my custom type in QML. How can I do this?

    Thanks
    Pieter

    raven-worxR 1 Reply Last reply
    0
    • Pieter CardoenP Pieter Cardoen

      Dear

      In Qt/QML, it is possible to define custom types from C++ and use them in QML by using:
      qmlRegisterType<CustomType>("ImportName",1,0,"TypeName");

      Qt also has support for the QVariant type which acts as an union for common datatypes. A custom type can be used with QVariant by using:
      Q_DECLARE_METATYPE(CustomType);

      Recently, I was doing implementations with listviews and treeviews where the model inherits from QAbstractItemModel and where this function is provided:
      virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const = 0

      I would like to use a custom type for visualizing an item of my ListView and it would be great if the data function could return a QVariant which can be converted to my custom type in QML. How can I do this?

      Thanks
      Pieter

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Pieter-Cardoen
      the qml engine only support this types: https://doc.qt.io/qt-5/qtqml-cppintegration-data.html

      if you want to support a custom type you will have to convert it to one of those. before you can process them in QML.
      For example a struct can be converted to a QVariantMap, or a custom class must interhit QObject and use the meta object system (properties, invokabkle methods, etc.)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      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