Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7584885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:02:05+00:00 2026-05-30T19:02:05+00:00

I was working on the code which in simple word displays a list of

  • 0

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.
I mean i have added some four Items.but i cannot see it in the output.Please help.

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

 #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

#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

#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();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:02:07+00:00Added an answer on May 30, 2026 at 7:02 pm

    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, e.g.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have jquery validation code which is working fine in ff but the same
I have a simple application which was working fine until I added some code
Simple code which is copied from web doesnt seem to be working. There is
I am working with some code which outputs a 3x3 rotation matrix and a
I have code which as been working against an older Active Directory server and
I am working on a piece of code which uses regular expressions in c.
I have the follwing code (which is not working): private void Window_PreviewKeyDown(object sender, KeyEventArgs
I'm working with legacy Java code which returns java.lang.object. I'm passing it into a
I'm working with the jQuery Validation plugin and I wrote the following code which
Hi Javascript gurus, I have this Javascript code which is working fine on Firefox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.