Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Parameter Qt::ItemDataRoles doesn't matchs with the expected values.
Forum Updated to NodeBB v4.3 + New Features

[Solved]Parameter Qt::ItemDataRoles doesn't matchs with the expected values.

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Charlie_Hdz
    wrote on last edited by
    #1

    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:

    !http://i1344.photobucket.com/albums/p656/Charlie_Hdz/qtproblem_zpsusvgeyqt.png(http://i1344.photobucket.com/albums/p656/Charlie_Hdz/qtproblem_zpsusvgeyqt.png)!

    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&lt;AudioMetaData&gt; 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
    @

    Kind Regards,
    Enrique Hernandez
    gearstech.com.mx
    chernandez@gearstech.com.mx

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Charlie_Hdz
      wrote on last edited by
      #2

      And If you have some suggestion about my code style, It will be appreciated : )

      Kind Regards,
      Enrique Hernandez
      gearstech.com.mx
      chernandez@gearstech.com.mx

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        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;
        }
        }
        @

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Charlie_Hdz
          wrote on last edited by
          #4

          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?

          Kind Regards,
          Enrique Hernandez
          gearstech.com.mx
          chernandez@gearstech.com.mx

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Charlie_Hdz
            wrote on last edited by
            #5

            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.

            Kind Regards,
            Enrique Hernandez
            gearstech.com.mx
            chernandez@gearstech.com.mx

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              [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

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Charlie_Hdz
                  wrote on last edited by
                  #8

                  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();
                  }@

                  !http://i1344.photobucket.com/albums/p656/Charlie_Hdz/qtproblem1_zpssgh3yzid.png(http://i1344.photobucket.com/albums/p656/Charlie_Hdz/qtproblem1_zpssgh3yzid.png)!

                  So I'll continue with my projects...

                  Thank you everyone.

                  Kind Regards,
                  Enrique Hernandez
                  gearstech.com.mx
                  chernandez@gearstech.com.mx

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved