QT之ComboBox角色使用QAbstractListModel
实例如下,
comboxlistnodel.h文件
#ifndef COMBOXLISTNODEL_H#define COMBOXLISTNODEL_H#include #include class IolData{public: IolData(QVariant name,QVariant type,QVariant aval,QVariant sf,QVariant acdh, QVariant acdb,QVariant acds,QVariant a0,QVariant a1,QVariant a2) { m_iol_name = name; m_iol_type = type; m_iol_a = aval; m_iol_sf = sf; m_iol_acdh = acdh; m_iol_acdb = acdb; m_iol_acds = acds; m_iol_a0 = a0; m_iol_a1 = a1; m_iol_a2 = a2; }public: QVariant m_iol_name; QVariant m_iol_type; QVariant m_iol_a; QVariant m_iol_sf; QVariant m_iol_acdh; QVariant m_iol_acdb; QVariant m_iol_acds; QVariant m_iol_a0; QVariant m_iol_a1; QVariant m_iol_a2;};class comboxlistnodel: public QAbstractListModel{ Q_OBJECT Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged)public: enum IolRoles {//枚举角色 NameRole = 0, TypeRole, ARole, SfRole, AcdHRole, AcdBRole, AcdSRole, A0Role, A1Role, A2Role }; int rowCount(const QModelIndex &parent = QModelIndex()) const; //QAbstractListModel是抽象类,所以需要实现下面两个函数 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; explicit comboxlistnodel(QObject *parent = 0); ~comboxlistnodel(); int currentIndex(); void setCurrentIndex(int index);signals: void currentIndexChanged();protected: QHash roleNames() const;private: QList m_items; int m_current_index; QVariant roleData( IolData item,int role)const;};#endif // COMBOXLISTNODEL_H
这里是comboxlistnodel.cpp文件
#include "comboxlistnodel.h"int comboxlistnodel::rowCount(const QModelIndex &parent) const{ return m_items.count();}QVariant comboxlistnodel::data(const QModelIndex &index, int role) const{ if ((index.row() < 0) || (index.row() >= m_items.count())) { return QVariant(); } return roleData(m_items.at(index.row()),role);}comboxlistnodel::comboxlistnodel(QObject *parent){m_current_index = 0;IolData a(118,"aa","aa","aa","aa","aa","aa","aa","aa","aa");IolData b(119,"aa","aa","aa","aa","aa","aa","aa","aa","aa");IolData c(110,"aa","aa","aa","aa","aa","aa","aa","aa","aa");IolData d(111,"aa","aa","aa","aa","aa","aa","aa","aa","aa");m_items.push_back(a);m_items.push_back(b);m_items.push_back(c);m_items.push_back(d);}comboxlistnodel::~comboxlistnodel(){}int comboxlistnodel::currentIndex(){ return m_current_index;}void comboxlistnodel::setCurrentIndex(int index){ m_current_index = index; emit currentIndexChanged();}QHash comboxlistnodel::roleNames() const//设置角色名{ QHash roles; roles[NameRole] = "IolName"; roles[TypeRole] = "IolType"; roles[ARole ] = "IolA"; roles[SfRole ] = "IolSf"; roles[AcdHRole] = "IolAcdH"; roles[AcdBRole] = "IolAcdB"; roles[AcdSRole] = "IolAcdS"; roles[A0Role ] = "IolA0"; roles[A1Role ] = "IolA1"; roles[A2Role ] = "IolA2"; return roles;}QVariant comboxlistnodel::roleData(IolData item, int role) const{ QVariant ret; switch (role) { case NameRole: ret = item.m_iol_name; break; case TypeRole: ret = item.m_iol_type; break; case ARole: ret = item.m_iol_a; break; case SfRole: ret = item.m_iol_sf; break; case AcdHRole: ret = item.m_iol_acdh; break; case AcdBRole: ret = item.m_iol_acdb; break; case AcdSRole: ret = item.m_iol_acds; break; case A0Role: ret = item.m_iol_a0; break; case A1Role: ret = item.m_iol_a1; break; case A2Role: ret = item.m_iol_a2; break; default: ret = QVariant(); break; } return ret;}
main.cpp中
comboxlistnodel *appc=new comboxlistnodel();
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("appc", appc );
qml中:
Rectangle{width: idViewTester.width; height: 60;color: "#3A3223";
ComboBox {
textRole: "IolName"
model:appc
}
}
效果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~