I've taken a look at Pixelator example and built my own model based on ImageModel class. Now, how do I embedd this functionality into my class, which code is:
#ifndef CSHOPPINGCARTMODEL_H_
#define CSHOPPINGCARTMODEL_H_
// qt includes
#include <QAbstractTableModel>
#include <QList>
#include <QString>
#include <QSize>
// custom includes
//#include "CMerchandizeOrder.h"
//#include "globals.h"
typedef struct
{
qint16 iMerchandizeID;
qreal rMerchandizePrice;
qint16 iMerchandizeQuantity;
qreal rSubtotal;
} structOrder;
typedef QList<structOrder> structOrders;
{
Q_OBJECT
public:
CShoppingCartModel
(QObject* parent
=0);
void setData(const structOrders& merchandizeOrders);
int rowCount
(const QModelIndex
& parent
=QModelIndex()) const;
int columnCount
(const QModelIndex
& parent
=QModelIndex()) const;
QVariant headerData
(int section, Qt
::Orientation orientation,
int role
= Qt
::DisplayRole) const;
private:
structOrders m_ModelData;
};
#endif /*CSHOPPINGCARTMODEL_H_*/
#ifndef CSHOPPINGCARTMODEL_H_
#define CSHOPPINGCARTMODEL_H_
// qt includes
#include <QAbstractTableModel>
#include <QList>
#include <QString>
#include <QSize>
// custom includes
//#include "CMerchandizeOrder.h"
//#include "globals.h"
typedef struct
{
qint16 iMerchandizeID;
QString strMerchandizeName;
qreal rMerchandizePrice;
qint16 iMerchandizeQuantity;
qreal rSubtotal;
} structOrder;
typedef QList<structOrder> structOrders;
class CShoppingCartModel : public QAbstractTableModel
{
Q_OBJECT
public:
CShoppingCartModel(QObject* parent=0);
void setData(const structOrders& merchandizeOrders);
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;
private:
structOrders m_ModelData;
};
#endif /*CSHOPPINGCARTMODEL_H_*/
To copy to clipboard, switch view to plain text mode
and it's implementation:
#include "CShoppingCartModel.h"
CShoppingCartModel
::CShoppingCartModel(QObject* parent
){
}
void CShoppingCartModel::setData(const structOrders& merchandizeOrder)
{
m_ModelData=merchandizeOrder;
reset();
}
int CShoppingCartModel::rowCount(const QModelIndex& /* parent */) const
{
return m_ModelData.size();
}
int CShoppingCartModel::columnCount(const QModelIndex& /* parent */) const
{
return 1;
}
QVariant CShoppingCartModel
::data(const QModelIndex
& index,
int role
) const {
// invalid order setup
structOrder invalidOrder;
invalidOrder.iMerchandizeID=0;
invalidOrder.iMerchandizeQuantity=0;
invalidOrder.rMerchandizePrice=0.00;
invalidOrder.rSubtotal=0.00;
invalidOrder.
strMerchandizeName=QString("");
/*
if (!index.isValid() || role!=Qt::DisplayRole)
return invalidOrder;
//return qGray(modelImage.pixel(index.column(), index.row()));
return m_ModelData.at(index.column());
*/
}
QVariant CShoppingCartModel
::headerData(int /* section */,
Qt::Orientation /* orientation */,
int role) const
{
if (role==Qt::SizeHintRole)
}
#include "CShoppingCartModel.h"
CShoppingCartModel::CShoppingCartModel(QObject* parent)
: QAbstractTableModel(parent)
{
}
void CShoppingCartModel::setData(const structOrders& merchandizeOrder)
{
m_ModelData=merchandizeOrder;
reset();
}
int CShoppingCartModel::rowCount(const QModelIndex& /* parent */) const
{
return m_ModelData.size();
}
int CShoppingCartModel::columnCount(const QModelIndex& /* parent */) const
{
return 1;
}
QVariant CShoppingCartModel::data(const QModelIndex& index, int role) const
{
// invalid order setup
structOrder invalidOrder;
invalidOrder.iMerchandizeID=0;
invalidOrder.iMerchandizeQuantity=0;
invalidOrder.rMerchandizePrice=0.00;
invalidOrder.rSubtotal=0.00;
invalidOrder.strMerchandizeName=QString("");
/*
if (!index.isValid() || role!=Qt::DisplayRole)
return invalidOrder;
//return qGray(modelImage.pixel(index.column(), index.row()));
return m_ModelData.at(index.column());
*/
return QVariant();
}
QVariant CShoppingCartModel::headerData(int /* section */,
Qt::Orientation /* orientation */,
int role) const
{
if (role==Qt::SizeHintRole)
return QSize(1, 1);
return QVariant();
}
To copy to clipboard, switch view to plain text mode
As you can see, I do not know how to return whole record as QVariant.
Bookmarks