This is all Qt 4.8:
There is a custom model, FileCopyItemModel, that implements the QAbstractItemModel to hold a list of files that need to be copied. The list of files and their properties are stored in another class (ImageCopyDTO) so QList<ImageCopyDTO> is the actual collection of data. When a QTableView's model is set to an instance of FileCopyItemModel, the columns get their names from headerData(), but no data is ever displayed. The QList<ImageCopyDTO> has elements, but the FileCopyItemModel::data() never gets called. Any idea why?
FileCopyItemModel.h
typedef QList<ImageCopyDTO> ImageCopyDTOList;
{
Q_OBJECT
public:
int relativePathNo() const { return 0; }
int noCopyNo() const { return 1; }
int imageName2CopyNo() const { return 2; }
int filesCopyCntNo() const { return 3; }
int qtyNo() const { return 4; }
int productNameNo() const { return 5; }
int notesNo() const { return 6; }
explicit FileCopyItemModel
(QObject *parent
= 0);
~FileCopyItemModel();
void clear() { _data.clear(); }
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const override;
Qt
::ItemFlags flags
(const QModelIndex &index
) const override;
// classic
void append(const ImageCopyDTO& element) { _data.append(element); }
ImageCopyDTOList getElementsFromData() const { return _data; }
private:
ImageCopyDTOList _data;
};
typedef QList<ImageCopyDTO> ImageCopyDTOList;
class FileCopyItemModel: public QAbstractItemModel
{
Q_OBJECT
public:
int relativePathNo() const { return 0; }
int noCopyNo() const { return 1; }
int imageName2CopyNo() const { return 2; }
int filesCopyCntNo() const { return 3; }
int qtyNo() const { return 4; }
int productNameNo() const { return 5; }
int notesNo() const { return 6; }
explicit FileCopyItemModel(QObject *parent = 0);
~FileCopyItemModel();
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const ;
virtual QModelIndex parent(const QModelIndex &child) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
void clear() { _data.clear(); }
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
// classic
void append(const ImageCopyDTO& element) { _data.append(element); }
ImageCopyDTOList getElementsFromData() const { return _data; }
private:
ImageCopyDTOList _data;
};
To copy to clipboard, switch view to plain text mode
FileCopyItemModel
::FileCopyItemModel(QObject *parent
){
}
FileCopyItemModel::~FileCopyItemModel(void)
{
}
QModelIndex FileCopyItemModel
::index(int row,
int column,
const QModelIndex &parent
/*= QModelIndex()*/) const {
}
{
}
int FileCopyItemModel
::rowCount(const QModelIndex &parent
/*= QModelIndex()*/) const {
return _data.count();
}
int FileCopyItemModel
::columnCount(const QModelIndex &parent
/*= QModelIndex()*/) const {
return 7;
}
bool FileCopyItemModel
::setData( const QModelIndex & index,
const QVariant & value,
int role
/*= Qt::EditRole*/ ) {
bool result = false;
int row = index.row();
int column = index.column();
if (!index.isValid() || row >= rowCount() || column >= columnCount())
{
return result;
}
if(role == Qt::EditRole)
{
switch (column) {
case 1:
_data[row].setNoCopy(value.toBool());
result = true;
break;
case 2:
_data[row].setImageName2Copy(value.toString());
result = true;
break;
case 3:
_data[row].setFilesCopyCnt(value.toInt());
result = true;
break;
default:
break;
} // end switch column
}
return result;
}
{
int row = index.row();
int column = index.column();
if (!index.isValid() || row >= rowCount() || column >= columnCount())
{
return result;
}
switch (role)
{
case Qt::DisplayRole:
// checking row and column to get data
switch (column) {
case 0:
result = _data.at(row).relativePath();
break;
case 1:
result = _data.at(row).noCopy();
break;
case 2:
result = _data.at(row).imageName2Copy();
break;
case 3:
result = _data.at(row).filesCopyCnt();
break;
case 4:
result = _data.at(row).qty();
break;
case 5:
result = _data.at(row).productName();
break;
case 6:
result = _data.at(row).notes();
break;
default:
break;
} // end switch column
break;
default:
break;
}
return result;
}
QVariant FileCopyItemModel
::headerData(int section, Qt
::Orientation orientation,
int role
/*= Qt::DisplayRole*/) const {
if (Qt::DisplayRole == role && Qt::Horizontal == orientation)
{
switch (section)
{
case 0:
result = "Copy";
break;
case 1:
result = "Image Gallery";
break;
case 2:
result = "Image";
break;
case 3:
result = "File Count";
break;
case 4:
result = "Item Qty";
break;
case 5:
result = "Product";
break;
case 6:
result = "Notes";
break;
default:
break;
}
}
return result;
}
Qt
::ItemFlags FileCopyItemModel
::flags(const QModelIndex &index
) const {
return result;
}
FileCopyItemModel::FileCopyItemModel(QObject *parent)
: QAbstractItemModel(parent)
{
}
FileCopyItemModel::~FileCopyItemModel(void)
{
}
QModelIndex FileCopyItemModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const
{
return QModelIndex();
}
QModelIndex FileCopyItemModel::parent(const QModelIndex &child) const
{
return QModelIndex();
}
int FileCopyItemModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return _data.count();
}
int FileCopyItemModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return 7;
}
bool FileCopyItemModel::setData( const QModelIndex & index, const QVariant & value, int role /*= Qt::EditRole*/ )
{
bool result = false;
int row = index.row();
int column = index.column();
if (!index.isValid() || row >= rowCount() || column >= columnCount())
{
return result;
}
if(role == Qt::EditRole)
{
switch (column) {
case 1:
_data[row].setNoCopy(value.toBool());
result = true;
break;
case 2:
_data[row].setImageName2Copy(value.toString());
result = true;
break;
case 3:
_data[row].setFilesCopyCnt(value.toInt());
result = true;
break;
default:
break;
} // end switch column
}
return result;
}
QVariant FileCopyItemModel::data(const QModelIndex &index, int role) const
{
QVariant result;
int row = index.row();
int column = index.column();
if (!index.isValid() || row >= rowCount() || column >= columnCount())
{
return result;
}
switch (role)
{
case Qt::DisplayRole:
// checking row and column to get data
switch (column) {
case 0:
result = _data.at(row).relativePath();
break;
case 1:
result = _data.at(row).noCopy();
break;
case 2:
result = _data.at(row).imageName2Copy();
break;
case 3:
result = _data.at(row).filesCopyCnt();
break;
case 4:
result = _data.at(row).qty();
break;
case 5:
result = _data.at(row).productName();
break;
case 6:
result = _data.at(row).notes();
break;
default:
break;
} // end switch column
break;
default:
break;
}
return result;
}
QVariant FileCopyItemModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
{
QVariant result = QVariant();
if (Qt::DisplayRole == role && Qt::Horizontal == orientation)
{
switch (section)
{
case 0:
result = "Copy";
break;
case 1:
result = "Image Gallery";
break;
case 2:
result = "Image";
break;
case 3:
result = "File Count";
break;
case 4:
result = "Item Qty";
break;
case 5:
result = "Product";
break;
case 6:
result = "Notes";
break;
default:
break;
}
}
return result;
}
Qt::ItemFlags FileCopyItemModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags result = Qt::ItemIsEditable | QAbstractItemModel::flags(index);
return result;
}
To copy to clipboard, switch view to plain text mode
Right now a pointer to the instance is being passed into the dialog with the QTableView. In the constructor this is being executed right now:
ui.fileCopyTableView->setModel(_model);
ui.fileCopyTableView->show();
ui.fileCopyTableView->setModel(_model);
ui.fileCopyTableView->show();
To copy to clipboard, switch view to plain text mode
Bookmarks