複数列のツリー ビューに QAbstractItemModel を実装しようとしています。各列は qstring ですが、今まで GUI にデータが表示されず、理由がわかりません。助けてください。
#ifndef PACKETLISTMODEL_H
#define PACKETLISTMODEL_H
#include <QAbstractItemModel>
#include<QVector>
#include<QStringList>
class PacketListModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit PacketListModel(QObject *parent = 0);
~PacketListModel();
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
void clear();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
void appendPacket(QStringList*);
QStringList* getPacket(int row);
signals:
public slots:
private:
QVector<QStringList*> pkts;
};
#endif // PACKETLISTMODEL_H
================================================== ============================
#include "packetlistmodel.h"
#include<QStandardItem>
PacketListModel::PacketListModel(QObject *parent) :
QAbstractItemModel(parent)
{
}
QModelIndex PacketListModel::index(int row, int column, const QModelIndex &parent) const{
Q_UNUSED(parent);
if (row >= pkts.count() || row < 0 || column >= 8)
return QModelIndex();
QStringList *pkt = pkts[row];
return createIndex(row, column, pkt);
}
QModelIndex PacketListModel::parent(const QModelIndex &index) const
{
Q_UNUSED(index);
return QModelIndex();
}
void PacketListModel::clear() {
beginResetModel();
pkts.clear();
endResetModel();
}
int PacketListModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() >= 8)
return 0;
return pkts.size();
}
int PacketListModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 8;
}
QVariant PacketListModel::data(const QModelIndex &index, int role) const{
if (!index.isValid())
return QVariant();
if (index.row() >= pkts.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole) {
// QStringList pkt=static_cast<QStringList>(index.internalPointer());
QStringList *pkt=pkts.at(index.row());
if (index.column() == 0)
return pkt->at(0);
else if (index.column() == 1)
return pkt->at(1);
else if (index.column() == 2)
return pkt->at(2);
else if (index.column() == 3)
return pkt->at(3);
else if (index.column() == 4)
return pkt->at(4);
else if (index.column() == 5)
return pkt->at(5);
else if (index.column() == 6)
return pkt->at(6);
else if (index.column() == 7)
return pkt->at(7);
}
return QVariant();
}
QVariant PacketListModel::headerData(int section, Qt::Orientation orientation,
int role /*= Qt::DisplayRole*/) const{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0) {
return tr("Num");
} else if (section == 1) {
return tr("Time");
} else if (section == 2) {
return tr("Length");
} else if (section == 3) {
return tr("Source IP");
} else if (section == 4) {
return tr("Source Port");
} else if (section == 5) {
return tr("Protocol");
} else if (section == 6) {
return tr("Destination IP");
} else if (section == 7) {
return tr("Destination Port");
}
}
return QVariant();
}
void PacketListModel::appendPacket(QStringList*pkt){
if(pkt->size()==8){
beginInsertRows(QModelIndex(), pkts.size(), pkts.size());
pkts.append(pkt);
endInsertRows();
}
}
QStringList* PacketListModel::getPacket(int row){
if(row >= 0 || row <= pkts.count())
return pkts[row];
}
PacketListModel::~PacketListModel(){
pkts.clear();
}