[Solved]Parameter Qt::ItemDataRoles doesn't matchs with the expected values.
-
Hi guys.
I'm working in a application where I make a custom model from QAbstractItemModel, in this I have a container of a custom class, and I like to show that data in a QTableView. But in the beginning the view dont show the values of the items, so I'm looking over the overrided data function.
@#include "playlistmodel.h"
PlayListModel::PlayListModel()
{
m_lstAudioMetaObj.clear();}
PlayListModel::~PlayListModel()
{}
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){ //!
if(index.column()== 0){
QVariant v(m_lstAudioMetaObj[index.row()].artist());
return v;
}
}
else
return QVariant();
}bool PlayListModel::insertData(const AudioMetaData &audio){
int tempSize=m_lstAudioMetaObj.size(); m_lstAudioMetaObj.append(audio); if(m_lstAudioMetaObj.size() >tempSize) return true; return false;
}
QVariant PlayListModel::headerData(int section, Qt::Orientation orientation, int role) const{
if(orientation != Qt::Horizontal || role != Qt::DisplayRole)
return QAbstractItemModel::headerData(section,orientation,role);switch(section){ case 0: return "Name"; case 1: return "Release Time"; case 2: return "Artist"; case 3: return "Album"; case 4: return "Genre"; case 5: return "Rating"; case 6: return "Duration"; } return QVariant();
}
@In the line 27 I'm trying to show the data of the respectly AudioMetaData, but the view isn't works and shows:
Then, I looked more deeply and found that the expected roles values wasn't matchs with the expected values (Qt::DisplayRole), throwing the following values:
Name Release Artist Album Genre Duration Rating
(Role Value) 6 7 9 10 1 8 ?
Notice that the last column(rating) is ignore and continue with the next row (tested)...So the questions are...
1.-How or Where can I set the values of each AudioMetaValue?
2.- And if that isn't possible. What do you suggest to fix it?
Thank You.
Your trusly, Carlos Hernandez. Wiwiped.
PS. If you need them, I give you the definitions and implementations of the others classes.
@#ifndef PLAYLISTMODEL_H
#define PLAYLISTMODEL_H#include <QAbstractTableModel>
#include <QModelIndex>
#include <QList>
#include <QVariant>
#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
int rowCount(const QModelIndex &parent= QModelIndex()) const;
int columnCount(const QModelIndex &parent=QModelIndex()) const;
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
bool insertData(const AudioMetaData& audio);
QVariant headerData(int section, Qt::Orientation orientation, int role) const;QList<AudioMetaData> list()const{return m_lstAudioMetaObj;}
private:
QList<AudioMetaData>m_lstAudioMetaObj;
};#endif // PLAYLISTMODEL_H
@@#include "audiometadata.h"
AudioMetaData::AudioMetaData()
{
qRegisterMetaType<AudioMetaData>("AudioMetaData");
}AudioMetaData::~AudioMetaData(){}
AudioMetaData::AudioMetaData(const AudioMetaData &other){
*this=other;
}
@@#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 If you have some suggestion about my code style, It will be appreciated : )
-
you just don return anything for Qt::DisplayRole in your data() method.
So why should the view display anything? :)Maybe it's a typo but shouldn't it be look like this?!
@
if(role == Qt::DisplayRole){
if(index.column()== 0){
QVariant v(m_lstAudioMetaObj[index.row()].artist());
return v;
}
}
@ -
Thank you raven.
Well, that part of code is so because I was testing
I'm expeting values of Qt::DisplayRole role, but instead that I saw that values vary as this:
For name column, role value is 6
For release column, role value is 7
...
Duration column, role value is 8
and for rating column, role value is invalidated.So, the questions are:
Why role values vary as that form when I'm expecting only Qt::DisplayRole because AudioMetaData members are all QString unless Rating (int) ?
How can I ensure Qt::DisplayRole role with my AudioMetaData?
-
bq. Maybe it’s a typo but shouldn’t it be look like this?!
Yes Indeed, But I saw that that if isn't work so I changed the setence.
-
[quote author="Charlie_Hdz" date="1422380725"]Why role values vary as that form when I'm expecting only Qt::DisplayRole because AudioMetaData members are all QString unless Rating (int) ?[/quote]The View will always ask for data from many different roles. This is expected.
For example:
- It asks for Qt::DisplayRole (0) to get the text
- It asks for Qt::BackgroundRole (8) to get the cell colour
- It asks for Qt::ForegroundRole (9) to get the text colour
-
Hi,
DisplayRole is only one part of what's needed to show something in a view. The other roles comprises font, text color, background color etc. That's why when you don't hand a particular role you return the value of the base class implementation.
-
I understand now.
I read some documentation as "Model/View Tutorial":http://qt-project.org/doc/qt-4.8/modelview.html and "Model/View Programming":http://qt-project.org/doc/qt-4.8/model-view-programming.html#creating-new-models again
So I only make some changes and everything work excellent.
@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){
int column=index.column(); int row=index.row();switch(column){ case 0: return QString("%1").arg(m_lstAudioMetaObj.at(row).name()); case 1: return QString("%1").arg(m_lstAudioMetaObj.at(row).dateOfRelease() .toString()); case 2: return QString("%1").arg(m_lstAudioMetaObj.at(row).artist()); case 3: return QString("%1").arg(m_lstAudioMetaObj.at(row).album()); case 4: return QString("%1").arg(m_lstAudioMetaObj.at(row).genre()); case 5: return QString("%1").arg(m_lstAudioMetaObj.at(row).duration().toString()); case 6: return QString("%1").arg(m_lstAudioMetaObj.at(row).rating()); }
}
else
return QVariant();
}@So I'll continue with my projects...
Thank you everyone.