Ich hab mir mein eigenes TableModel geschrieben, aber es wird weder der Header noch der Inhalt angezeigt. Woran kann das liegen?
Model
* Created on 06.01.2010
*
* Copyright (c) 2009, Kay Czarnotta
*
* All rights reserved.
*/
#ifndef SIGNALTABLEMODEL_H
#define SIGNALTABLEMODEL_H
#include <QAbstractItemModel>
#include <QVector>
#include "TSignal.h"
class SignalTableModel : public QAbstractItemModel
{
public:
SignalTableModel();
QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex() ) const;
QModelIndex parent(const QModelIndex& index ) const;
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex() ) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole ) const;
bool setData (const QModelIndex& index, const QVariant& value, int role = Qt::EditRole );
Qt::ItemFlags flags(const QModelIndex& index ) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
void addSignal(int column, Signal* sig);
void setColumnCount(int count);
protected:
QVector< QVector<Signal*>* >* tableData;
QVector<QString> header;
};
#endif // SIGNALTABLEMODEL_H
/*
* Created on 06.01.2010
*
* Copyright (c) 2009, Kay Czarnotta
*
* All rights reserved.
*/
#include "SignalTableModel.h"
#include "Settings.h"
#include <QModelIndex>
#include <QDebug>
SignalTableModel::SignalTableModel()
{
tableData = new QVector< QVector<Signal*>* >();
}
QModelIndex SignalTableModel::index(int row, int column, const QModelIndex& parent) const
{
qDebug()<< "[M] index (" << row << "," << column << ") " << parent;
QModelIndex index;
if(tableData->count() > column && tableData->at(column)->count() > row)
index = createIndex(row, column, tableData->at(column)->at(row));
qDebug() << "[R] Index " << index;
return index;
}
QModelIndex SignalTableModel::parent(const QModelIndex& index ) const
{
qDebug()<< "[M] parent " << index;
QModelIndex ret;
if(tableData->count()>index.column() && tableData->at(index.column())->count()>index.row()
&& index.row() > 0)
ret = createIndex(index.row()-1, index.column(), tableData->at(index.column())->at(index.row()-1));
qDebug() << "[R] Parent " << ret;
return ret;
}
int SignalTableModel::rowCount(const QModelIndex& parent) const
{
qDebug()<< "[M] rowCount "<< parent;
int rowCount = 0;
if(tableData->count() > parent.column() && parent.column() >= 0)
rowCount = tableData->at(parent.column())->count();
qDebug() << "[R] RowCount: " << rowCount;
return rowCount;
}
int SignalTableModel::columnCount(const QModelIndex& parent) const
{
qDebug()<< "[M] columnCount "<< parent;
int colCount = tableData->count();
qDebug() << "[R] ColumnCount: " << colCount;
return colCount;
}
QVariant SignalTableModel::data(const QModelIndex& index, int role) const
{
if(tableData->count()<=index.column() || tableData->at(index.column())->count()<=index.row())
return "";
Signal* sig = tableData->at(index.column())->at(index.row());
if(index.column()%2==0)
return sig->getRacer();
else
return sig->getTime();
}
bool SignalTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if(tableData->count()<=index.column() || tableData->at(index.column())->count()<=index.row())
return false;
if(index.column()%2==0)
{
QString s = value.toString();
tableData->at(index.row())->at(index.row())->setRacer(s);
}
emit dataChanged(index, index);
return true;
}
Qt::ItemFlags SignalTableModel::flags(const QModelIndex& index ) const
{
if(index.column()%2==0)
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
else
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
QVariant SignalTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
qDebug()<< "[M] headerData "<< section << "," << orientation << "," << role;
qDebug() << "[R] Header: " << header.at(section);
return header.at(section);
}
void SignalTableModel::addSignal(int column, Signal* sig)
{
if(tableData->count()<=column)
return;
tableData->at(column)->append(sig);
QModelIndex index = createIndex(column, tableData->at(column)->count(), sig);
emit dataChanged(index, index);
reset();
}
void SignalTableModel::setColumnCount(int count)
{
if(tableData->count()!=0)
{
for(int i=0;i<tableData->count();i++)
delete tableData->at(i);
tableData->clear();
header.clear();
}
for(int i=0;i<count;i++)
{
tableData->append(new QVector<Signal*>());
QString s = "abc " + QString::number(i);
//setHeaderData(i,Qt::Horizontal,i/*%2==0?"":Settings::getSlotName(i)*/, Qt::DisplayRole);
header.append(s/*%2==0?"":Settings::getSlotName(i)*/);
}
reset();
emit headerDataChanged(Qt::Horizontal, 0, count);
}
Verwendet wirds hier
qDebug() << "+++++++++++++++++++++++++++++++";
this->signalTimeModel = new SignalTableModel();
this->ui->timesTable->setModel(signalTimeModel);
qDebug() << "...............................";
qDebug() << "------------------------";
signalTimeModel->setColumnCount(colCount);
qDebug() << "========================";