Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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?


  • Lifetime Qt Champion

    @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



  • 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");
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

  • Lifetime Qt Champion

    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."


Log in to reply