Built-in type casting to QVariant error.



  • Hi guys.

    I'm creating a model to show some AudioMetaData,so I created a class AudioMetaData:

    @#ifndef AUDIOMETADATA_H
    #define AUDIOMETADATA_H
    #include <QObject>
    #include <QString>
    #include <QDate>
    #include <QTime>

    class AudioMetaData
    {
    public:
    explicit AudioMetaData(QString* n,QDate* d,QString* a,QString* al,
    QString* g,QTime* du,int* r):m_name(n),m_dateOfRelease(d),
    m_artist(a),m_album(al),m_genre(g),m_duration(du),m_Rating(r){}

    //!
    AudioMetaData();
    ~AudioMetaData();
    AudioMetaData(const AudioMetaData& other);
    
    //Access Function.
    QString name()const{return *m_name;}
    void setName(QString* name){m_name=name;}
    QDate dateOfRelease()const{return *m_dateOfRelease;}
    void setDate(QDate* date){m_dateOfRelease=date;}
    QString artist()const{return *m_artist;}
    void setArtist(QString* artist){m_artist=artist;}
    QString album()const{return *m_album;}
    void setAlbum(QString* album){m_album=album;}
    QString genre()const{return *m_genre;}
    void setGenre(QString* genre){m_genre=genre;}
    QTime duration()const{return *m_duration;}
    void setDuration(QTime* duration){m_duration=duration;}
    

    private:
    QString* m_name;
    QDate* m_dateOfRelease;
    QString* m_artist;
    QString* m_album;
    QString* m_genre;
    QTime* m_duration;
    int* m_Rating;
    };

    Q_DECLARE_METATYPE(AudioMetaData)
    #endif // AUDIOMETADATA_H@

    And the model too...

    @#ifndef PLAYLISTMODEL_H
    #define PLAYLISTMODEL_H

    #include <QAbstractTableModel>
    #include <QModelIndex>
    #include <QList>
    #include <QVariant>
    #include <QMetaEnum>
    #include "audiometadata.h"

    class PlayListModel : public QAbstractTableModel
    {
    Q_OBJECT
    public:
    PlayListModel(QList<AudioMetaData*> lst, QObject* parent=0):
    QAbstractTableModel(parent),m_lstAudioMetaObj(lst){}
    PlayListModel();
    ~PlayListModel();
    //Implemented Function
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    int rowCount(const QModelIndex &parent= QModelIndex()) const;
    int columnCount(const QModelIndex &parent=QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
    private:
    QList<AudioMetaData*>m_lstAudioMetaObj;
    };

    #endif // PLAYLISTMODEL_H
    @

    and its implementations:

    @#include "playlistmodel.h"

    PlayListModel::PlayListModel()
    {

    setHeaderData(0,Qt::Horizontal,QString("Name"),Qt::DisplayRole);
    

    }

    PlayListModel::~PlayListModel()
    {

    }

    QVariant PlayListModel::headerData(int section, Qt::Orientation orientation, int role) const{

    }

    int PlayListModel::rowCount(const QModelIndex &parent) const{
    return m_lstAudioMetaObj.size();
    }

    int PlayListModel::columnCount(const QModelIndex &parent) const{
    return 7;
    }

    QVariant PlayListModel::data(const QModelIndex &index, int role) const{
    if(!index.isValid())
    return QVariant();
    if(index.row()>=m_lstAudioMetaObj.size())
    return QVariant();
    if(role==Qt::DisplayRole){
    qRegisterMetaType<AudioMetaData>("AudioMetaData");//Desesperate Try
    return QVariant::fromValue(m_lstAudioMetaObj[index.row()]);//Here is the beast
    }
    else
    return QVariant();

    }
    @

    Well, the problem is in the line 34 of the PlayListModel.cpp, Im trying to return a QVariant modeling a AudioMetaData from a QList...but the compiler set me this error:

    error: invalid application of 'sizeof' to incomplete type 'QStaticAssertFailure<false>'
    enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, COUNTER) = sizeof(QStaticAssertFailure<!!(Condition)>)}

    I'll be very appreciate if someone can help me.

    Thank You.



  • Hi,

    Your list does not contain AudioMetaData objects but pointers to AudioMetaData objects, but you register AudioMetaData with qRegisterMetaType, not a pointer.

    By the way why is all your private data in AudioMetaData pointers? You don't any pointers there. Just use values. QString is "implicitly shared":http://doc.qt.io/qt-5/shared.html so you can treat it like an int and QTime is sizeof(int). It looks like m_Rating is not an array. If it is I'd suggest that you replace it with a vector, otherwise just use an int.



  • Hi ckakman, I changed the pointersto values and the register of AudioMetaData too, as you said, and everthing is working now.

    Thanks a lot


Log in to reply
 

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