我正在尝试为多列树视图实现QAbstractItemModel每列都是一个qstring但是GUI中没有数据出现到现在并且不知道为什么,请帮助plz
#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();
}
答案 0 :(得分:0)
#include "packetlistmodel.h"
#include<QStandardItem>
#include<QDebug>
PacketListModel::PacketListModel(QObject *parent) :
QAbstractItemModel(parent)
{
}
QModelIndex PacketListModel::index(int row, int column, const QModelIndex &parent) const{
Q_UNUSED(parent);
if (row >= pkts.size() || row < 0 || column >= 8)
return QModelIndex();
QStringList pkt = pkts.at(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
{
////////no children/////////////////////////
if(parent.isValid())
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 && index.isValid()) {
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();
}
bool PacketListModel::setData(const QModelIndex &index,const QVariant &value, int role)
{
bool result = false;
if (index.isValid() && role == Qt::EditRole) {
QStringList pkt =value.toString().split("\t");
int idx = index.row();
pkts.replace(idx, pkt);//problem was here it should take value not pointer
emit dataChanged(index, index);
result = true;
}
return result;
}
bool PacketListModel::insertRow(int position, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginInsertRows(QModelIndex(), position, position);
QStringList qs;
pkts.insert(position,qs);
endInsertRows();
return true;
}
PacketListModel::~PacketListModel(){
pkts.clear();
}