I have implemented as you suggested it to me...
But after I compiled...I tried to run the program...and I got a big memory leake of Qt libraries...dunno why
...
MsgStatsModel
::MsgStatsModel(QObject *parent
){
// std::cout <<"Model Created!"<<"\n";
}
MsgStatsModel::~MsgStatsModel()
{
// std::cout <<"Model Destroyed!"<<"\n";
}
int MsgStatsModel
::rowCount(const QModelIndex &parent
) const {
Q_UNUSED(parent);
return idStatsList.count();
}
int MsgStatsModel
::columnCount(const QModelIndex &parent
) const {
Q_UNUSED(parent);
return 2;
}
QVariant MsgStatsModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if(orientation == Qt::Horizontal){
if(role == Qt::DisplayRole){
switch(section){
case 0 :
return tr("ID");
break;
case 1 :
return tr("Count");
break;
case 2 :
return tr("Comment");
break;
default :
break;
}
}
}
return ret;
}
{
if(!index.isValid()){
std::cout <<"Invalid Index of data provided!"<< std::endl;
return ret;
}
int row = index.row();
int col = index.column();
if(row >= 0 && row < rowCount()) {
if(role == Qt::DisplayRole) {
CountStats item = idStatsList[row];
switch(col) {
case 0 :
ret = item.id;
break;
case 1 :
ret = item.count;
break;
case 2 :
ret = item.comments;
break;
default :
break;
}
}
}
return ret;
}
//SLOT catching new arriving messages
void MsgStatsModel::updateStats(const QVcaCanMsg &canmsg)
{
int id = canmsg.id();
int old_row_count = idStatsList.count();
if(!idStatsList.isEmpty()){
if(indexHash.contains(id)){
int row = indexHash.value(id);
int idCount = idStatsList[row].count;
idStatsList[row].count = idCount+1;
qDebug() << "UPDATED COUNT IS: " << idStatsList[row].count;
emit dataChanged(index(row, 0), index(row, 0));
} else {
CountStats item;
item.id=id;
item.count=1;
int row = old_row_count;
idStatsList.append(item);
indexHash.insert(id, row);
endInsertRows();
}
} else {
CountStats item;
item.id=id;
item.count=1;
int row = old_row_count;
idStatsList.append(item);
indexHash.insert(id, row);
endInsertRows();
}
}
MsgStatsModel::MsgStatsModel(QObject *parent)
: QAbstractTableModel(parent)
{
// std::cout <<"Model Created!"<<"\n";
}
MsgStatsModel::~MsgStatsModel()
{
// std::cout <<"Model Destroyed!"<<"\n";
}
int MsgStatsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return idStatsList.count();
}
int MsgStatsModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant MsgStatsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
QVariant ret = QAbstractTableModel::headerData(section, orientation, role);
if(orientation == Qt::Horizontal){
if(role == Qt::DisplayRole){
switch(section){
case 0 :
return tr("ID");
break;
case 1 :
return tr("Count");
break;
case 2 :
return tr("Comment");
break;
default :
break;
}
}
}
return ret;
}
QVariant MsgStatsModel::data(const QModelIndex &index, int role) const
{
QVariant ret;
if(!index.isValid()){
std::cout <<"Invalid Index of data provided!"<< std::endl;
return ret;
}
int row = index.row();
int col = index.column();
if(row >= 0 && row < rowCount()) {
if(role == Qt::DisplayRole) {
CountStats item = idStatsList[row];
switch(col) {
case 0 :
ret = item.id;
break;
case 1 :
ret = item.count;
break;
case 2 :
ret = item.comments;
break;
default :
break;
}
}
}
return ret;
}
//SLOT catching new arriving messages
void MsgStatsModel::updateStats(const QVcaCanMsg &canmsg)
{
int id = canmsg.id();
int old_row_count = idStatsList.count();
if(!idStatsList.isEmpty()){
if(indexHash.contains(id)){
int row = indexHash.value(id);
int idCount = idStatsList[row].count;
idStatsList[row].count = idCount+1;
qDebug() << "UPDATED COUNT IS: " << idStatsList[row].count;
emit dataChanged(index(row, 0), index(row, 0));
} else {
CountStats item;
item.id=id;
item.count=1;
int row = old_row_count;
beginInsertRows(QModelIndex(), row, row);
idStatsList.append(item);
indexHash.insert(id, row);
endInsertRows();
}
} else {
CountStats item;
item.id=id;
item.count=1;
int row = old_row_count;
beginInsertRows(QModelIndex(), row, row);
idStatsList.append(item);
indexHash.insert(id, row);
endInsertRows();
}
}
To copy to clipboard, switch view to plain text mode
header:
typedef struct countstats{
int id;
int count;
} CountStats;
{
Q_OBJECT
typedef QList<CountStats> StatList;
QHash<int, int> indexHash;
public:
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const;
StatList idStatsList;
public slots:
void updateStats(const QVcaCanMsg &canmsg);
public:
MsgStatsModel
(QObject *parent
= 0);
~MsgStatsModel();
};
typedef struct countstats{
int id;
int count;
QString comments;
} CountStats;
class MsgStatsModel : public QAbstractTableModel
{
Q_OBJECT
typedef QList<CountStats> StatList;
QHash<int, int> indexHash;
public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
StatList idStatsList;
public slots:
void updateStats(const QVcaCanMsg &canmsg);
public:
MsgStatsModel(QObject *parent = 0);
~MsgStatsModel();
};
To copy to clipboard, switch view to plain text mode
so no success
Bookmarks