标签:dex 方法 use bsp spec 问题 ... 自定义类 类对象
1 //Parameter.h 2 //加粗单词为成员变量 3 //假设数据库中某表有三个字段Index,Name,Describe 4 5 class Parameter : public QObject 6 { 7 Q_OBJECT 8 public: 9 explicit Parameter( QObject *parent = 0); 10 11 Q_INVOKABLE QVariant getIndex() const { return index;} //用Q_INVOKABLE声明后才能被元对象(QMetaObject)调用 12 Q_INVOKABLE void setIndex(const QVariant &value) { index = value.toInt(); } 13 14 Q_INVOKABLE QVariant getName() const { return name; } 15 Q_INVOKABLE void setName(const QVariant &value) { name = value.toString(); } 16 17 Q_INVOKABLE QVariant getDecribe() const { return describe; } 18 Q_INVOKABLE QVariant setDescribe(const QVariant &value) { describe = value; } 19 20 QMap<int, int> getMethodGETIndexs()const;//获得“取值器”函数(即getXX函数) 的索引值列表,这些函数都被Q_INVOKABLE声明过 21 QMap<int, int> getMethodSETIndexs() const;//获得“设置器”函数(即setXX函数) 的索引值列表,这些函数都被Q_INVOKABLE声明过 22 23 private: 24 void setMethodGETIndexs(); //设置“取值器”函数(即getXX函数) 的索引值列表,这些函数都被Q_INVOKABLE声明过 25 void setMethodSETIndexs(); //设置“设置器”函数(即setXX函数) 的索引值列表,这些函数都被Q_INVOKABLE声明过 26 27 static int getNewIndex(); 28 29 int index; 30 QString name; 31 QString describe; 32 33 QMap<int,int> methodGETIndexs; 34 QMap<int,int> methodSETIndexs; 35 };
1 //Parameter.cpp 2 3 Parameter::Parameter(QObject *parent) : 4 QObject(parent), 5 index(getNewIndex()), 6 name("Unnamed"), 7 describe("") 8 { 9 setMethodGETIndexs(); 10 setMethodSETIndexs(); 11 } 12 13 void Parameter::setMethodGETIndex() 14 { 15 int index1 = this->metaObject()->indexOfMethod("getIndex()"); 16 methodGETIndexs.insert(0,index1); 17 18 int index2 = this->metaObject()->indexOfMethod("getName()"); 19 methodGETIndexs.insert(1,index2); 20 21 int index3 = this->metaObject()->indexOfMethod("getDecribe()"); 22 methodGETIndexs.insert(2,index3); 23 24 } 25 26 void Parameter::setMethodSETIndexs() 27 { 28 int index1 = this->metaObject()->indexOfMethod("setIndex(QVariant)"); 29 methodSETIndexs.insert(0,index1); 30 31 int index2 = this->metaObject()->indexOfMethod("setName(QVariant)"); 32 methodSETIndexs.insert(1,index2); 33 34 int index3 = this->metaObject()->indexOfMethod("setDescribe(QVariant)"); 35 methodSETIndexs.insert(2,index3); 36 } 37 38 QMap<int, int> Parameter::getMethodSETIndexs() const 39 { 40 return methodSETIndexs; 41 } 42 43 QMap<int, int> Parameter::getMethodGETIndexs() const 44 { 45 return methodGETIndexs; 46 } 47 48 int Parameter::getNewIndex() 49 { 50 //查询数据库 51 //返回最新的Index字段 52 }
1 //TableModel.h 2 class TableModel : public QAbstractTableModel 3 { 4 Q_OBJECT 5 public: 6 explicit TableModel(QObject *parent = 0); 7 int rowCount(const QModelIndex &parent = QModelIndex()) const;// 8 int columnCount(const QModelIndex &parent = QModelIndex()) const; 9 QVariant data(const QModelIndex &index, int role) const; 10 bool setData(const QModelIndex &index, const QVariant &value, int role); 11 private: 12 static QList<Parameter*> getTableParameters(); //该函数用于初始化dataParameters 13 14 QVariant specificIndexValue(const QModelIndex &index) const; 15 int getMethodGETIndex(const QModelIndex &index) const; 16 QMetaMethod getMetaMethod(const QModelIndex &index,int methodIndex) const; 17 bool setSpecificData(const QModelIndex &index, const QVariant &value); 18 int getMethodSETIndex(const QModelIndex &index); 19 20 QList<Parameter*> dataParameters; //存储从数据库表中查询所得的值,每个Parameter对象代表一条记录
//tablemodel.cpp
TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent), dataParameters(getTableParameters()) { } static TableModel::QList<Parameter*> getTableParameters() { //查询数据库,返回字段值列表 } int TableModel::rowCount(const QModelIndex &parent = QModelIndex()) { dataParameters.size(); } int TableModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); return dataParameters.getMethodGETIndexs().size(); } QVariant TableModel::data(const QModelIndex &index, int role) const { if(!index.isValid()){ return QVariant(); } return specificData(index,role); } QVariant TableModel::specificData(const QModelIndex &index, int role)const { switch(role) { case Qt::TextAlignmentRole: return int(Qt::AlignHCenter | Qt::AlignVCenter); case Qt::DisplayRole: return specificIndexValue(index); case Qt::EditRole: return specificIndexValue(index); default: return QVariant(); } return QVariant(); } QVariant TableModel::specificIndexValue(const QModelIndex &index) const { QVariant retValue; int methodIndex = methodGETIndex(index); QMetaMethod getMethod = getMetaMethod(index,methodIndex); getMethod.invoke(dataParameters.at(index.row()),Qt::DirectConnection,Q_RETURN_ARG(QVariant,retValue)); return retValue; } int TableModel::getMethodGETIndex(const QModelIndex &index) const { int methodIndex = dataParameters.at(index.row())->getMethodGETIndexs().value(index.column()); return methodIndex; } QMetaMethod TableModel::getMetaMethod(const QModelIndex &index,int methodIndex) const { QMetaMethod method = dataParameters.at(index.row())->metaObject()->method(methodIndex); return method; } bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if(!isIndexValid(index)) return false; if(role == Qt::EditRole && setSpecificData(index,value)) ResParameters::instance().modifyRecord(index); return QAbstractTableModel::setData(index,value); } bool TableModel::setSpecificData(const QModelIndex &index, const QVariant &value) { if( specificIndexValue(index) != value){ int methodIndex = getMethodSETIndex(index); QMetaMethod setMethod = getMetaMethod( index, methodIndex); return setMethod.invoke( dataParameters.at(index.row()), Qt::DirectConnection, Q_ARG(QVariant,value) ); } return false; } int TableModel::getMethodSETIndex(const QModelIndex &index) { int methodIndex = dataParameters.at(index.row())->getMethodSETIndexs().value(index.column()); return methodIndex; }
标签:dex 方法 use bsp spec 问题 ... 自定义类 类对象
原文地址:http://www.cnblogs.com/sfy5848/p/6030493.html