简体   繁体   English

QT模型在QT中为空

[英]QObject Model is empty in QT

I was working on the code which in simple word displays a list of items. 我正在编写代码,用简单的单词显示项目列表。 i am able to populate the QlistView but its a empty model.The model i have created is empty. 我能够填充QlistView,但它是一个空模型。我创建的模型是空的。 I mean i have added some four Items.but i cannot see it in the output.Please help. 我的意思是我添加了四个Items.but我在输出中看不到它。请帮助。

Main.cpp Main.cpp的

#include <QtGui/QApplication>
#include <QDeclarativeContext>
#include <QKeyEvent>
#include <QAbstractItemModel>
#include <QListView>
 #include <QDebug>

#include "qmlapplicationviewer.h"
#include "listmodel.h"
#include "songitem.h"
#include "mainwindow.h"
#include "song.h"
#include "songs.h"
#include "songitem.h"


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

ListModel *model = new ListModel(new SongItem, qApp);
model->appendRow(new SongItem( "Michel Telo","Ai Se Eu Te Pego","Ai Se Eu Te Pego", model));
SongItem test = new SongItem( "Michel Telo","Ai Se Eu Te Pego","Ai Se Eu Te Pego", model);
qDebug() << test.data(NameRole);
QModelIndex index = model->index(0, 2, QModelIndex());
qDebug() << model->data(index);
      Songs songz;
      Song s;
      vector<Song> songCollection;
      songCollection = songz.all(3);
      int ii;
      for(ii=0; ii < songCollection.size(); ii++)
      {
         QString qstr = QString::fromStdString(songCollection[ii].album);
         model->appendRow(new SongItem( qstr , qstr, qstr, model));

      }



QListView *view = new QListView;
view->setWindowTitle("Model Data");

view->setModel(model);
view->setStyleSheet("color: black");
view->show();

    return app.exec();
}

ListModel.cpp ListModel.cpp

 #include <QDebug>
#include "listmodel.h"
int i=0;
ListModel::ListModel(ListItem* prototype, QObject *parent) :
    QAbstractListModel(parent), m_prototype(prototype)
{
  setRoleNames(m_prototype->roleNames());
}

int ListModel::rowCount(const QModelIndex &parent) const
{
  Q_UNUSED(parent);
  return m_list.size();
}

QVariant ListModel::data(const QModelIndex &index, int role) const
{
  if(index.row() < 0 || index.row() >= m_list.size())
    return QVariant();

  return m_list.at(index.row())->data(role);
}

ListModel::~ListModel() {
  delete m_prototype;
  clear();
}

void ListModel::appendRow(ListItem *item)
{
  appendRows(QList<ListItem*>() << item);
  //qDebug() << "Test";
  //qDebug() << item;
}

void ListModel::appendRows(const QList<ListItem *> &items)
{
  beginInsertRows(QModelIndex(), rowCount(), rowCount()+items.size()-1);
  foreach(ListItem *item, items) {
    connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
    m_list.append(item);
  }
  endInsertRows();
}

void ListModel::insertRow(int row, ListItem *item)
{
  beginInsertRows(QModelIndex(), row, row);
  connect(item, SIGNAL(dataChanged()), SLOT(handleItemChange()));
  m_list.insert(row, item);
  endInsertRows();
}

void ListModel::handleItemChange()
{
  ListItem* item = static_cast<ListItem*>(sender());
  QModelIndex index = indexFromItem(item);
  if(index.isValid())
    emit dataChanged(index, index);
}

ListItem * ListModel::find(const QString &id) const
{
  foreach(ListItem* item, m_list) {
    if(item->id() == id) return item;
      }
  return 0;
}

QModelIndex ListModel::indexFromItem(const ListItem *item) const
{
  Q_ASSERT(item);
  for(int row=0; row<m_list.size(); ++row) {
    if(m_list.at(row) == item) return index(row);
  }
  return QModelIndex();
}

void ListModel::clear()
{
      qDeleteAll(m_list);
  m_list.clear();
}

bool ListModel::removeRow(int row, const QModelIndex &parent)
{
  Q_UNUSED(parent);
  if(row < 0 || row >= m_list.size()) return false;
  beginRemoveRows(QModelIndex(), row, row);
  delete m_list.takeAt(row);
  endRemoveRows();
  return true;
}

bool ListModel::removeRows(int row, int count, const QModelIndex &parent)
{
  Q_UNUSED(parent);
  if(row < 0 || (row+count) >= m_list.size()) return false;
  beginRemoveRows(QModelIndex(), row, row+count-1);
  for(int i=0; i<count; ++i)
  {
    delete m_list.takeAt(row);
  }
  endRemoveRows();
  return true;
}

ListItem * ListModel::takeRow(int row)
{
  beginRemoveRows(QModelIndex(), row, row);
  ListItem* item = m_list.takeAt(row);
  endRemoveRows();
  return item;
}

SongItem.h SongItem.h

#ifndef SONGITEM_H
#define SONGITEM_H

#include "listmodel.h"

class SongItem : public ListItem
{
    Q_OBJECT
public:
    enum Roles {
      NameRole = Qt::UserRole+1,
      ArtistRole,
      TrackRole
    };

public:
    SongItem(QObject *parent = 0): ListItem(parent){}
    explicit SongItem(const QString &name, const QString &artist, const QString &track, QObject *parent = 0);
    QVariant data(int role) const;

    QHash<int, QByteArray> roleNames() const;

    inline QString id() const { return m_name; }
    inline QString name() const { return m_name; }
    inline QString artist() const { return m_artist; }
    inline QString track() const { return m_track; }

  private:
    QString m_name;
    QString m_artist;
    QString m_track;

};

#endif // SONGITEM_H

SongItem.cpp SongItem.cpp

#include "songitem.h"
 #include <QDebug>

SongItem::SongItem(const QString &name, const QString &artist, const QString &track, QObject *parent) :
    ListItem(parent), m_name(name), m_artist(artist), m_track(track)
{
 }


QHash<int, QByteArray> SongItem::roleNames() const
{
  QHash<int, QByteArray> names;
  names[NameRole] = "name";
  names[ArtistRole] = "artist";
  names[TrackRole] = "track";
  return names;
}

QVariant SongItem::data(int role) const
{
  switch(role) {
  case NameRole:
    return name();
  case ArtistRole:
    return artist();
  case TrackRole:
    return track();
  default:
    return QVariant();
  }
}

A QListView will fetch data with the Qt::DisplayRole role for display, so you have to adapt SongItem::data to return something for this role, eg QListView将获取具有Qt :: DisplayRole角色的数据以供显示,因此您必须调整SongItem :: data以返回此角色的内容,例如

QVariant SongItem::data(int role) const
{
  switch(role) {
  case Qt::DisplayRole:
    return name();
  case NameRole:
    return name();
  case ArtistRole:
    return artist();
  case TrackRole:
    return track();
  default:
    return QVariant();
  }
}

It might be that you took some examples related to QML, where it works quite differently. 可能是您采用了一些与QML相关的示例,它的工作方式完全不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM