当前位置: 代码迷 >> QT开发 >> 自定义Model绑定View之后数据不显示的有关问题
  详细解决方案

自定义Model绑定View之后数据不显示的有关问题

热度:146   发布时间:2016-04-25 03:16:09.0
自定义Model绑定View之后数据不显示的问题
最近在做音乐播放器。为简化问题模型简化如下
使用基于QAbstractListModel的自定义MediaPlayListModel用来管理QMediaPlayList其中listview部分显示的为歌曲的名称,但是实现不了,第一次发帖,求大神指教问题出在哪了。其中自定义的MediaPlayListModel中已经实现了data,rowCount等功能,默认初始化时候播放列表是空的,需要通过选定含有mp3文件的路径才能加载源代码如下。希望大神指出问题出在哪了。
main.cpp
#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();

    return a.exec();
}

listmodel.h
#ifndef LISTMODEL_H
#define LISTMODEL_H

#include <QAbstractListModel>
#include<QMediaPlaylist>

class ListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    explicit ListModel(QObject *parent = 0);
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
    QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const;

    void addRow(QVariant var);
    void setPlayList(QMediaPlaylist& playList);
signals:

public slots:
private:
    QMediaPlaylist listCon;

};

#endif // LISTMODEL_H

listmodel.cpp
#include "listmodel.h"
#include<QAbstractListModel>
ListModel::ListModel(QObject *parent) :
    QAbstractListModel(parent)
{
}

void ListModel::addRow(QVariant var)
{

    QString szItem=var.toString();
    QUrl urlItem=QUrl::fromLocalFile(szItem);
    QMediaContent conItem(urlItem);
    this->listCon.addMedia(conItem);
}

int ListModel::columnCount(const QModelIndex &parent) const
{
    return 1;
}

QVariant ListModel::data(const QModelIndex &index, int role) const
{
    if(index.isValid()==false)
    {
        return QVariant();
    }
    else
    {
        switch(role)
        {
        case Qt::DisplayRole:
        case Qt::EditRole:
        {
            QMediaContent mdaItem=this->listCon.media(index.row());
            QUrl urlItem=mdaItem.canonicalUrl();
            QString szItem=urlItem.fileName();
            return QVariant(szItem);
        break;
        }
        }
    }
}

QModelIndex ListModel::index(int row, int column, const QModelIndex &parent) const
{
    QModelIndex idx=this->createIndex(row,column);
    return idx;
}

int ListModel::rowCount(const QModelIndex &parent) const
{
    int nRow=this->listCon.mediaCount();
    return nRow;
}

bool ListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    int nIndex=index.row();
    QString szItem=value.toString();
    QUrl urlItem=QUrl::fromLocalFile(szItem);
    QMediaContent mdaItem=QMediaContent(urlItem);
    switch (role)
    {
    case Qt::DisplayRole:
    case Qt::EditRole:
    {

        this->listCon.removeMedia(nIndex);
        this->listCon.insertMedia(nIndex-1,mdaItem);
        QModelIndex idxBegin=this->index(nIndex-1,0);
        QModelIndex idxEnd=this->index(nIndex,0);
        emit dataChanged(idxBegin,idxEnd);
    }
    }
}

void ListModel::setPlayList(QMediaPlaylist &playList)
{
    this->beginResetModel();
  相关解决方案