gpt4 book ai didi

c++ - QObject模型在QT中为空

转载 作者:行者123 更新时间:2023-11-30 03:03:10 26 4
gpt4 key购买 nike

我正在编写用简单的词显示项目列表的代码。我能够填充 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();
}

列表模型.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

歌曲项目.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();
}
}

最佳答案

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 相关的示例,它们的工作方式完全不同。

关于c++ - QObject模型在QT中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9532381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com