C++ QoObject模型在QT中为空

C++ QoObject模型在QT中为空,c++,qt,C++,Qt,我正在编写代码,用简单的词显示一个项目列表。 我可以填充QlistView,但它是一个空模型。我创建的模型是空的。 我的意思是我添加了四个项目,但是我在输出中看不到。请帮助 Main.cpp #include <QtGui/QApplication> #include <QDeclarativeContext> #include <QKeyEvent> #include <QAbstractItemModel> #include <QList

我正在编写代码,用简单的词显示一个项目列表。 我可以填充QlistView,但它是一个空模型。我创建的模型是空的。 我的意思是我添加了四个项目,但是我在输出中看不到。请帮助

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();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括“qmlapplicationviewer.h”
#包括“listmodel.h”
#包括“songitem.h”
#包括“mainwindow.h”
#包括“song.h”
#包括“songs.h”
#包括“songitem.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
ListModel*model=new ListModel(new SongItem,qApp);
模型->附录行(新歌曲项(“米歇尔·泰洛”,“艾-塞-欧-特-佩戈”,“艾-塞-欧-特-佩戈”,模型));
SongItem测试=新的SongItem(“Michel Telo”,“Ai Se Eu Te Pego”,“Ai Se Eu Te Pego”,模型);
qDebug()索引(0,2,QModelIndex());
qDebug()数据(索引);
宋兹歌曲;
宋s;
媒介歌曲集;
songCollection=songz.all(3);
int ii;
对于(ii=0;ii附录行(新项目(qstr、qstr、qstr、模型));
}
QListView*视图=新的QListView;
查看->设置窗口标题(“模型数据”);
查看->设置模型(模型);
查看->设置样式表(“颜色:黑色”);
查看->显示();
返回app.exec();
}
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;
}
#包括
#包括“listmodel.h”
int i=0;
ListModel::ListModel(ListItem*原型,QObject*父对象):
QAbstractListModel(父级),m_原型(原型)
{
setRoleNames(m_原型->roleNames());
}
int ListModel::行计数(常量QModelIndex&parent)常量
{
Q_未使用(父母);
返回m_list.size();
}
QVariant ListModel::数据(常量QModelIndex&index,int角色)常量
{
如果(index.row()<0 | | index.row()>=m_list.size())
返回QVariant();
返回m_list.at(index.row())->data(role);
}
ListModel::~ListModel(){
删除m_原型;
清除();
}
作废ListModel::appendRow(ListItem*item)
{

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

可能是你举了一些与QML相关的例子,在那里它的工作方式完全不同。

什么是
ListModel
definition?什么是
index
definition?thankz Lol4t0..得到了解决方案这是太多的代码了。请下次发布一个测试用例。thankz Koying…我是新手。我正在编写一些代码来学习QT和C++谢谢。
#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();
  }
}
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();
  }
}