Convert Custom Class to QVariant



  • I read that with Q_DECLARE_METATYPE i can add Custom Classes to the accepted Templates of QVariant. But i was thinking i can return now my Custom Class to a QVariant. This is my Code.

    QVariant DiaryTableModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if (role == Qt::DisplayRole)
        {
            if (index.column() == 0)
                return int(mItems.at(index.row())->Id);
            else if (index.column() == 1)
                return QString(mItems.at(index.row())->Date);
            else if (index.column() == 2)
                return StarTable(mItems.at(index.row())->Rating);
        }
    
        return QVariant();
    }
    

    But my Debugger says that StarTable cannot be converted to a QVariant. Why? StarTable has Q_DECLARE_METATYPE(StarTable)


  • Moderators

    @Fuel

    QVariant::fromValue<StarTable>( starTableVar );
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.