ich habe ein listview mit model, im model sind daten vorhanden, und data() gibt auch daten zurück, nur im listview wird nichts angezeigt, hab ich event. etwas übersehen?
Code: Alles auswählen
#ifndef LISTVIEWTESTMODEL_H
#define LISTVIEWTESTMODEL_H
#include <QAbstractListModel>
#include <QStringList>
class ListViewTestModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit ListViewTestModel(QObject *parent = 0);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent) const;
bool addTestData(QString testData);
private:
QStringList list_data;
};
#endif // LISTVIEWTESTMODEL_H
Code: Alles auswählen
#include "listviewtestmodel.h"
ListViewTestModel::ListViewTestModel(QObject *parent) : QAbstractListModel(parent)
{
}
QVariant ListViewTestModel::data(const QModelIndex &index, int role) const
{
Q_UNUSED(role)
if(!index.isValid())
return QVariant();
if(list_data.count() < index.row())
return QVariant();
QString row_data = list_data.at(index.row());
return row_data;
}
bool ListViewTestModel::addTestData(QString testData)
{
beginInsertRows(QModelIndex(), list_data.size(), list_data.size());
list_data.append(testData);
endInsertRows();
return true;
}
int ListViewTestModel::rowCount(const QModelIndex &parent) const
{
if(parent.isValid())
return 0;
return list_data.count();
}
Code: Alles auswählen
ui->setupUi(this);
lv_model = new ListViewTestModel();
ui->listView->setModel(lv_model);
lv_model->addTestData("Test1");
lv_model->addTestData("Test2");
lv_model->addTestData("Test3");
lv_model->addTestData("Test4");
lv_model->addTestData("Test5");
lv_model->addTestData("Test6");
lv_model->addTestData("Test7");