Important: Please read the Qt Code of Conduct -

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(>Id);
            else if (index.column() == 1)
                return QString(>Date);
            else if (index.column() == 2)
                return StarTable(>Rating);
        return QVariant();

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

  • Moderators


    QVariant::fromValue<StarTable>( starTableVar );

Log in to reply