Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change the return type of model->data() function from QVariant to custom type
QtWS25 Last Chance

Change the return type of model->data() function from QVariant to custom type

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 786 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.
  • M Offline
    M Offline
    MaxBec
    wrote on last edited by
    #1

    Hey guys,

    i have written my custom implementation of QAbstractTableModel. Of course i've implemented the virtual function model->data()

    QVariant MAVHilStateModel::data(const QModelIndex &index, int role) const
    {
    
    	if(index.isValid())
    		return QVariant();
    	if (index.row() >= hil_state_list.size())
    		   return QVariant();
    
    	qDebug() << hil_state_list.at(index.row());
    
    	if (role == Qt::DisplayRole)
    		  return hil_state_list.at(index.row());
    	   else
    		  return QVariant();
    }
    

    Now i get the error message on the line where i want to return a custom type

    return hil_state_list.at(index.row())
    

    Error:

    MAVHilStateModel.cpp:47:12: error: conversion function from '__mavlink_hil_state_t *const' to 'QVariant' invokes a deleted function
    qvariant.h:497:12: note: 'QVariant' has been explicitly marked deleted here
    

    The custom type is defined in a third party library here:

    // Macro to define packed structures
    #ifdef __GNUC__
      #define MAVPACKED( __Declaration__ ) __Declaration__ __attribute__((packed))
    #else
      #define MAVPACKED( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop) )
    #endif
    
    
    MAVPACKED(
    typedef struct __mavlink_hil_state_t {
     uint64_t time_usec; /*< [us] Timestamp (UNIX Epoch time or time since system boot). The receiving end can infer timestamp format (since 1.1.1970 or since system boot) by checking for the magnitude the number.*/
     float roll; /*< [rad] Roll angle*/
     float pitch; /*< [rad] Pitch angle*/
     float yaw; /*< [rad] Yaw angle*/
     float rollspeed; /*< [rad/s] Body frame roll / phi angular speed*/
     float pitchspeed; /*< [rad/s] Body frame pitch / theta angular speed*/
     float yawspeed; /*< [rad/s] Body frame yaw / psi angular speed*/
     int32_t lat; /*< [degE7] Latitude*/
     int32_t lon; /*< [degE7] Longitude*/
     int32_t alt; /*< [mm] Altitude*/
     int16_t vx; /*< [cm/s] Ground X Speed (Latitude)*/
     int16_t vy; /*< [cm/s] Ground Y Speed (Longitude)*/
     int16_t vz; /*< [cm/s] Ground Z Speed (Altitude)*/
     int16_t xacc; /*< [mG] X acceleration*/
     int16_t yacc; /*< [mG] Y acceleration*/
     int16_t zacc; /*< [mG] Z acceleration*/
    }) mavlink_hil_state_t;
    

    What am i doing wrong? How can i change the return type of the data function?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MaxBec said in Change the return type of model->data() function from QVariant to custom type:

      How can i change the return type of the data function?

      QAbstractItemModel::data() returns a QVariant - you can't change this. What you can do is to put your custom struct into a QVariant. See QVariant::fromValue() and the documentation to Q_DECLARE_METATYPE

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • M Offline
        M Offline
        MaxBec
        wrote on last edited by
        #3

        I've put this declaration now in my main.cpp:

        Q_DECLARE_METATYPE(mavlink_hil_state_t);
        
        int main(int argc, char *argv[])
        {
        

        New error:

        /Applications/Qt/5.14.0/clang_64/lib/QtCore.framework/Headers/qmetatype.h:1810: Fehler: static_assert failed due to requirement 'bool(QMetaTypeId2<__mavlink_hil_state_t>::Defined)' "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system"
            Q_STATIC_ASSERT_X(QMetaTypeId2<T>::Defined, "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system");
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          From the doc: "Ideally, this macro should be placed below the declaration of the class or struct. If that is not possible, it can be put in a private header file which has to be included every time that type is used in a QVariant."

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3

          • Login

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