dolphins
18th October 2007, 02:28
What I'm trying to do is use a button in a dialog to add the struct data(XianStruct) in a reimplemented QAbstractListModel that is shown through a QTableView.
but I dont know how to call setData() function ? QTableView not refreshing after push the add button?
thanks! I have the data function as follows:
//Dialog.h
Model *ModelData;
//Dialog.cpp
ModelData = new Model(this);
void CParamDialog::on_ButtonAdd_clicked()
{
Model::XianStruct addMe;
ModelData->insertRows(0, 1);
ModelData->setData( ModelData->index(-1), &addMe, Qt::DisplayRole); ???
}
// model.h
class Model : public QAbstractListModel
{
Q_OBJECT
public:
Model(QObject *parent = 0);
~Model();
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
Qt::ItemFlags flags(const QModelIndex &index) const;
//Required to be able to resize the model (add & remove shit)
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
struct XianStruct
{
qint32 iZaShu, iFenZhi;
float fZhouBegin, fZhouEnd, fDiameter ;
//Make a quick constructor for our struct to have default values for a new item
XianStruct()
{
iZaShu = 50;
iFenZhi = 1;
fZhouBegin = 8.0;
fZhouEnd = 60.0;
fDiameter = 0.6;
}
};
private:
QList<XianStruct> _data;
};
Q_DECLARE_METATYPE(Model::XianStruct);
//model.cpp
QVariant Model::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
int row = index.row();
if ( row < 0 || row >= _data.count() )
return QVariant();
if (role == Qt::DisplayRole)
{
switch( index.column() ){
case 0: return _data[row].iZaShu;
case 1: return _data[row].iFenZhi;
case 2: return _data[row].fZhouBegin;
case 3: return _data[row].fZhouEnd;
case 4: return _data[row].fDiameter;
default: return _data[row].iZaShu;
}
}
else
return QVariant();
return QVariant();
}
bool Model::setData(const QModelIndex &index, const QVariant &value, int role )
{
if (index.isValid() && role == Qt::DisplayRole)
{
int row = index.row();
XianStruct newData = value.value<XianStruct>();
//get a pointer to the list item so we can change its contents
_data[row].iZaShu = newData.iZaShu;
_data[row].iFenZhi = newData.iFenZhi;
_data[row].fZhouBegin = newData.fZhouBegin;
_data[row].fZhouEnd = newData.fZhouEnd;
_data[row].fDiameter = newData.fDiameter;
emit dataChanged( index, index);
return true;
}
else return false;
}:confused:
but I dont know how to call setData() function ? QTableView not refreshing after push the add button?
thanks! I have the data function as follows:
//Dialog.h
Model *ModelData;
//Dialog.cpp
ModelData = new Model(this);
void CParamDialog::on_ButtonAdd_clicked()
{
Model::XianStruct addMe;
ModelData->insertRows(0, 1);
ModelData->setData( ModelData->index(-1), &addMe, Qt::DisplayRole); ???
}
// model.h
class Model : public QAbstractListModel
{
Q_OBJECT
public:
Model(QObject *parent = 0);
~Model();
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
Qt::ItemFlags flags(const QModelIndex &index) const;
//Required to be able to resize the model (add & remove shit)
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
struct XianStruct
{
qint32 iZaShu, iFenZhi;
float fZhouBegin, fZhouEnd, fDiameter ;
//Make a quick constructor for our struct to have default values for a new item
XianStruct()
{
iZaShu = 50;
iFenZhi = 1;
fZhouBegin = 8.0;
fZhouEnd = 60.0;
fDiameter = 0.6;
}
};
private:
QList<XianStruct> _data;
};
Q_DECLARE_METATYPE(Model::XianStruct);
//model.cpp
QVariant Model::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
int row = index.row();
if ( row < 0 || row >= _data.count() )
return QVariant();
if (role == Qt::DisplayRole)
{
switch( index.column() ){
case 0: return _data[row].iZaShu;
case 1: return _data[row].iFenZhi;
case 2: return _data[row].fZhouBegin;
case 3: return _data[row].fZhouEnd;
case 4: return _data[row].fDiameter;
default: return _data[row].iZaShu;
}
}
else
return QVariant();
return QVariant();
}
bool Model::setData(const QModelIndex &index, const QVariant &value, int role )
{
if (index.isValid() && role == Qt::DisplayRole)
{
int row = index.row();
XianStruct newData = value.value<XianStruct>();
//get a pointer to the list item so we can change its contents
_data[row].iZaShu = newData.iZaShu;
_data[row].iFenZhi = newData.iFenZhi;
_data[row].fZhouBegin = newData.fZhouBegin;
_data[row].fZhouEnd = newData.fZhouEnd;
_data[row].fDiameter = newData.fDiameter;
emit dataChanged( index, index);
return true;
}
else return false;
}:confused: