This demo model shows the problem. It starts with 6 rows and 4 columns and in the proxy model i remove 3 rows and add one column. The last column is created but there is no data in it, it's also not clickable.
#ifndef TestTableModel_H
#define TestTableModel_H
#include <QAbstractTableModel>
{
private:
typedef QVector<QVariant> RowData;
typedef QVector<RowData> ModelData;
public:
TestTableModel
(QObject *parent
= 0);
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
private:
void init();
private:
ModelData mModelData;
};
#endif // TestTableModel_H
#ifndef TestTableModel_H
#define TestTableModel_H
#include <QAbstractTableModel>
class TestTableModel : public QAbstractTableModel
{
private:
typedef QVector<QVariant> RowData;
typedef QVector<RowData> ModelData;
public:
TestTableModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
private:
void init();
private:
ModelData mModelData;
};
#endif // TestTableModel_H
To copy to clipboard, switch view to plain text mode
#include "TestTableModel.h"
{
init();
}
int TestTableModel
::rowCount(const QModelIndex &parent
) const {
if(parent.isValid())
return 0;
else
return mModelData.size();
}
int TestTableModel
::columnCount(const QModelIndex &parent
) const {
if(parent.isValid())
return 0;
else
{
if(mModelData.size() > 0)
return mModelData.at(0).size();
}
return 0;
}
{
if(role == Qt::DisplayRole)
{
if(index.isValid())
{
return mModelData.at(index.row()).at(index.column());
}
}
}
QVariant TestTableModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch(section)
{
case 0:
return "Value1";
case 1:
return "Value2";
case 2:
return "Value3";
case 3:
return "Value4";
default:
}
}
}
void TestTableModel::init()
{
RowData rdA;
rdA.append("A");
rdA.append(1000);
rdA.append(800);
rdA.append(100);
mModelData.append(rdA);
RowData rdB;
rdB.append("B");
rdB.append(2500);
rdB.append(2000);
rdB.append(300);
mModelData.append(rdB);
RowData rdC;
rdC.append("C");
rdC.append(1400);
rdC.append(1120);
rdC.append(100);
mModelData.append(rdC);
RowData rdA1;
rdA1.append("A");
rdA1.append(1200);
rdA1.append(960);
rdA1.append(180);
mModelData.append(rdA1);
RowData rdA2;
rdA2.append("A");
rdA2.append(1000);
rdA2.append(800);
rdA2.append(100);
mModelData.append(rdA2);
RowData rdB1;
rdB1.append("B");
rdB1.append(1500);
rdB1.append(1200);
rdB1.append(100);
mModelData.append(rdB1);
}
#include "TestTableModel.h"
TestTableModel::TestTableModel(QObject *parent) : QAbstractTableModel(parent)
{
init();
}
int TestTableModel::rowCount(const QModelIndex &parent) const
{
if(parent.isValid())
return 0;
else
return mModelData.size();
}
int TestTableModel::columnCount(const QModelIndex &parent) const
{
if(parent.isValid())
return 0;
else
{
if(mModelData.size() > 0)
return mModelData.at(0).size();
}
return 0;
}
QVariant TestTableModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::DisplayRole)
{
if(index.isValid())
{
return mModelData.at(index.row()).at(index.column());
}
}
return QVariant();
}
QVariant TestTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch(section)
{
case 0:
return "Value1";
case 1:
return "Value2";
case 2:
return "Value3";
case 3:
return "Value4";
default:
return QVariant();
}
}
return QVariant();
}
void TestTableModel::init()
{
RowData rdA;
rdA.append("A");
rdA.append(1000);
rdA.append(800);
rdA.append(100);
mModelData.append(rdA);
RowData rdB;
rdB.append("B");
rdB.append(2500);
rdB.append(2000);
rdB.append(300);
mModelData.append(rdB);
RowData rdC;
rdC.append("C");
rdC.append(1400);
rdC.append(1120);
rdC.append(100);
mModelData.append(rdC);
RowData rdA1;
rdA1.append("A");
rdA1.append(1200);
rdA1.append(960);
rdA1.append(180);
mModelData.append(rdA1);
RowData rdA2;
rdA2.append("A");
rdA2.append(1000);
rdA2.append(800);
rdA2.append(100);
mModelData.append(rdA2);
RowData rdB1;
rdB1.append("B");
rdB1.append(1500);
rdB1.append(1200);
rdB1.append(100);
mModelData.append(rdB1);
}
To copy to clipboard, switch view to plain text mode
And the proxy model
#ifndef TestTableProxyModel_H
#define TestTableProxyModel_H
#include <QSortFilterProxyModel>
{
private:
typedef QVector<QVariant> RowData;
typedef QVector<RowData> ModelData;
public:
TestTableProxyModel
(QObject *parent
= 0);
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
) const;
void refreshView();
private:
int mRowCount;
int mColumnCount;
ModelData mTmpModelData;
bool mUpdateDone;
};
#endif // TestTableProxyModel_H
#ifndef TestTableProxyModel_H
#define TestTableProxyModel_H
#include <QSortFilterProxyModel>
class TestTableProxyModel : public QSortFilterProxyModel
{
private:
typedef QVector<QVariant> RowData;
typedef QVector<RowData> ModelData;
public:
TestTableProxyModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QVariant data(const QModelIndex &index, int role) const;
void refreshView();
private:
QStringList mHeaderFields;
int mRowCount;
int mColumnCount;
ModelData mTmpModelData;
bool mUpdateDone;
};
#endif // TestTableProxyModel_H
To copy to clipboard, switch view to plain text mode
#include "TestTableProxyModel.h"
TestTableProxyModel
::TestTableProxyModel(QObject *parent
){
}
int TestTableProxyModel
::rowCount(const QModelIndex &parent
) const {
if(parent.isValid())
return 0;
if(mRowCount > 0 && mUpdateDone)
return mRowCount;
else
}
int TestTableProxyModel
::columnCount(const QModelIndex &parent
) const {
if(parent.isValid())
return 0;
if(mColumnCount > 0 && mUpdateDone)
return mColumnCount;
else
}
QVariant TestTableProxyModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if(mUpdateDone)
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
if(!mHeaderFields.isEmpty())
{
if(section < mHeaderFields.size())
return mHeaderFields.at(section);
else
}
}
}
}
{
if(mUpdateDone)
{
if(role == Qt::DisplayRole)
{
if(!mTmpModelData.isEmpty())
{
if(index.row() < mTmpModelData.size())
{
if(index.column() < mTmpModelData.at(index.row()).size())
return mTmpModelData.at(index.row()).at(index.column());
}
else
}
}
}
}
void TestTableProxyModel::refreshView()
{
mUpdateDone = false;
mRowCount = 3;
mColumnCount = 5;
for(int i=0;i<mColumnCount;i++)
for(int i=0;i<mRowCount;i++)
{
RowData rd;
for(int j=0;j<mColumnCount;j++)
mTmpModelData.append(rd);
}
mUpdateDone = true;
invalidate();
}
#include "TestTableProxyModel.h"
TestTableProxyModel::TestTableProxyModel(QObject *parent)
: QSortFilterProxyModel(parent), mRowCount(0), mColumnCount(0), mUpdateDone(true)
{
}
int TestTableProxyModel::rowCount(const QModelIndex &parent) const
{
if(parent.isValid())
return 0;
if(mRowCount > 0 && mUpdateDone)
return mRowCount;
else
return QSortFilterProxyModel::rowCount(parent);
}
int TestTableProxyModel::columnCount(const QModelIndex &parent) const
{
if(parent.isValid())
return 0;
if(mColumnCount > 0 && mUpdateDone)
return mColumnCount;
else
return QSortFilterProxyModel::columnCount(parent);
}
QVariant TestTableProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(mUpdateDone)
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
if(!mHeaderFields.isEmpty())
{
if(section < mHeaderFields.size())
return mHeaderFields.at(section);
else
return QVariant();
}
}
}
return QSortFilterProxyModel::headerData(section, orientation, role);
}
QVariant TestTableProxyModel::data(const QModelIndex &index, int role) const
{
if(mUpdateDone)
{
if(role == Qt::DisplayRole)
{
if(!mTmpModelData.isEmpty())
{
if(index.row() < mTmpModelData.size())
{
if(index.column() < mTmpModelData.at(index.row()).size())
return mTmpModelData.at(index.row()).at(index.column());
}
else
return QVariant();
}
}
}
return QSortFilterProxyModel::data(index, role);
}
void TestTableProxyModel::refreshView()
{
mUpdateDone = false;
mRowCount = 3;
mColumnCount = 5;
for(int i=0;i<mColumnCount;i++)
mHeaderFields.append(QString("Column").append(QString::number(i+1)));
for(int i=0;i<mRowCount;i++)
{
RowData rd;
for(int j=0;j<mColumnCount;j++)
rd.append(QString("Value").append(QString::number(i+1)).append("_").append(QString::number(j+1)));
mTmpModelData.append(rd);
}
mUpdateDone = true;
invalidate();
}
To copy to clipboard, switch view to plain text mode
Bookmarks