QMLã®äž»ãªç®çã¯ã€ã³ã¿ãŒãã§ãŒã¹ãäœæããããšã§ãããããMVCãã¿ãŒã³ã«åŸã£ãŠããã¬ãŒã³ããŒã·ã§ã³ãšå¶åŸ¡ãå®è£ ãããŸãã ã¢ãã«ã®å®è£ ã§ã¯ãC ++ã¯éåžžã«è«ççã§ãã ããã§ãå¶éãã¯ããã«å°ãªããªããè€éãªã¢ãã«ãå®è£ ã§ããããã«ãªããŸãã ããã«ãããã°ã©ã ã®å€§éšåãC ++ã§èšè¿°ãããŠãããããããããŒã¿ãååŸãããå Žåãã¢ãã«ãåãå Žæã«é 眮ããã®ãæåã§ãã
ãã®ãããªã¢ãã«ã®äœ¿çšã¯ãå®è£ ã®èŠããäžã®è€éãã«ãã£ãŠãã³ããŠãããããããŸããã C ++ãæãç°¡åãªèšèªã§ã¯ãªããšã¯èšããŸããã ããã¯QMLãããè€éã§ããã足å ã§èªåèªèº«ãæããªãããã«æ³šæãå¿ èŠã§ããããã¯äºå®ã§ãã ããã«ãããããããå®éã«ã¯ããã¹ãŠãããã»ã©æãããã§ã¯ãããŸããã
ãŸããçŽç²ãªC ++ã§æžããŠããã®ã§ã¯ãªããQtã䜿çšããŠããããšãå¿ããªãã§ãã ããã QObjectã®èŠªåãã³ã³ãããã·ã°ãã«ãã¹ãããã®æé»çãªå ±æãQVariantãªã©ãã¡ã¢ãªã䜿çšããäœæ¥ãå€§å¹ ã«ç°¡çŽ åããã³èªååããŸããããã«ãããéçºè ã¯é çãã解æŸãããä¿¡é Œæ§ãåäžããŸãã åçããã°ã©ãã³ã°èšèªã§æžããŠããããã«èŠããããšãããããŸãã ããã«ãããQMLãšC ++ã®éã®ã®ã£ãããå°ãããªããQMLãšC ++éã®ç§»è¡ãå€å°ã¹ã ãŒãºã«ãªããŸãã
第äºã«ããã¹ãŠã®QMLã¢ãã«ã¯æçµçã«ãããã®åãC ++ã¢ãã«ã«çž®å°ãããŸããæé«ã®ããã©ãŒãã³ã¹ã§ã¯ãªããåçŽåãããããŒãžã§ã³ã®ã¿ãååŸãããŸãã QMLã¢ãã«ã®æäœæ¹æ³ãæ¢ã«ç解ããŠããå Žåã¯ãC ++ã¢ãã«ãæ±ãæ¹ãç°¡åã§ãã ããã»ã¹ã§ããå°ãäœã¬ãã«ã®æ å ±ãåŠç¿ããã ãã§ãªãããããã©ã®ããã«æ©èœããããç解ããããšãæ¹åãããŸãã
äžè¬çã«ãC ++ã¢ãã«ããã¹ã¿ãŒãã䟡å€ã¯ãããŸãã ããã¯ç¹ã«ãQAbstractItemModelã«ã€ããŠåœãŠã¯ãŸããŸãã
QMLã®ã¢ãã«ãã¥ãŒïŒ
- QMLã®ã¢ãã«ãã¥ãŒã ããŒããŒããå ¥é
- QMLã®ã¢ãã«ãã¥ãŒã ããŒã1ïŒäºåäœæãããã³ã³ããŒãã³ããã¥ãŒ
- QMLã®ã¢ãã«ãã¥ãŒã ããŒã2ïŒã«ã¹ã¿ã ãã¥ãŒ
- QMLã®ã¢ãã«ãã¥ãŒã ããŒã3ïŒQMLããã³JavaScriptã®ã¢ãã«
- QMLã®ã¢ãã«ãã¥ãŒã ããŒã4ïŒC ++ã¢ãã«
1. C ++-QAbstractItemModelã¢ãã«
ããã¯ãQt Model-Viewãã¬ãŒã ã¯ãŒã¯ã®æšæºã¢ãã«ã§ãã ãã®ã¯ã©ã¹ã«ã¯è±å¯ãªæ©èœããããããŸããŸãªè€éãã®ã¢ãã«ãæ§ç¯ã§ããŸãã
ãã®ãããªã¢ãã«ã«ã¯3ã€ã®åºæ¬ã¯ã©ã¹ããããŸãã QAbstractTableModelã¯ããŒã¿ã衚圢åŒã§è¡šããè¡çªå·ãšåçªå·ã䜿çšããŠããŒã¿ã«ã¢ã¯ã»ã¹ããŸãã QAbstractListModelã¯ãªã¹ãå ã®ããŒã¿ãè¡šãã1ã€ã®åãæã€ä»¥åã®ã¢ãã«ã®ç¹æ®ãªã±ãŒã¹ãšèšããŸãã
ããã©ããããQAbstractItemModelã¯ããäžè¬åãããããŒãžã§ã³ã§ãã åããŒãã«èŠçŽ ã«ã¯ãåèŠçŽ ãå«ããããšãã§ããŸããåèŠçŽ ãããŒãã«åœ¢åŒã§ç·šæãããŸãã ãããã£ãŠããã®ããŒãã«ã䜿çšããŠãããªãŒæ§é ãæŽçã§ããŸãã Qtã«ã¯ãåã¯æåã®åã®èŠçŽ ããæãŠãªããšããäžè¬çãªã«ãŒã«ããããŸããQTreeViewãªã©ã®Qtããã®ãã¥ãŒã䜿çšããå Žåããã®åœ¢åŒãå¿ èŠã§ããã䟿å©ãªæ¹æ³ã§ã¢ãã«ãç·šæããããšãçŠæ¢ããŸããã ãã®ãããªã¢ãã«ã®äŸãšããŠãQFileSystemModelã¯ã©ã¹ãåŒçšã§ããŸãã æåã®åã¯ããã¡ã€ã«ãŸãã¯ãã£ã¬ã¯ããªã®ååã§ãã ãã£ã¬ã¯ããªã®å Žåããã®åã®èŠçŽ ã«ãåããããŸãã æ®ãã®åã«ã¯ããã¡ã€ã«ã«é¢ããããŸããŸãªæ å ±ïŒãµã€ãºãå€æŽæéãªã©ïŒãå«ãŸããŸãã ãã®ãããªããŒã¿æ§é ã¯ãä»»æã®ãã¡ã€ã«ãããŒãžã£ãŒã«ãããŸãã
ã¢ãã«ãšãã¥ãŒã®éã«ç¹å¥ãªãããã·ã¢ãã«ãæ¿å ¥ã§ããŸãã ãã®ãããªã¢ãã«ã¯ãã¡ã€ã³ã¢ãã«ãžã®åŒã³åºããã€ã³ã¿ãŒã»ããããç¹å®ã®èŠçŽ ãé ããããé åºãå€æŽããããããŒã¿ã®åä¿¡ãèšé²ã«åœ±é¿ãäžãããããããšãã§ããŸã Qtã«ã¯ãæ¢æã®QSortFilterProxyModelã¯ã©ã¹ããããã¢ãã«ããŒã¿ããœãŒãããã³/ãŸãã¯ãã£ã«ã¿ãŒããã圢åŒã§è¡šãããšãã§ããŸãã æ©èœãååã§ãªãå Žåã¯ããã®ã¯ã©ã¹ãŸãã¯QAbstractProxyModelããç¶æ¿ããŠãç¬èªã®ãããã·ã¢ãã«ãäœæã§ããŸãã
QMLã®ãã¥ãŒã¯ãªã¹ãã®ã¿ã衚瀺ã§ããŸãã VisualDataModelã䜿çšãããšãããªãŒæ§é ãããã²ãŒãã§ããŸãããçŸåšã®ã¬ãã«ã®èŠçŽ ã®ã¿ã衚瀺ã§ããŸãã ããŒã¿ãããªãŒã«ä¿åããŠQMLã§è¡šç€ºããå¿ èŠãããå Žåã¯ãVisualDataModelã䜿çšãããããã®ããªãŒããªã¹ãã«å€æããç¬èªã®ãããã·ã¢ãã«ãäœæããå¿ èŠããããŸãã
ç¬èªã®ã¢ãã«ãäœæããã«ã¯ãã¢ãã«ã®åºæ¬ã¯ã©ã¹ã®1ã€ããç¶æ¿ãããã®ã¢ãã«ã«å¿ èŠãªã¡ãœããã決å®ããå¿ èŠããããŸãã äœãããå¿ èŠãããããç°¡åã«èª¬æããŸããããã詳现ãªæ å ±ã¯ããã¥ã¡ã³ãã§å ¥æã§ããŸã ã è€éãã®æé ã§æ€èšããŸãã
ãªã¹ãã¢ãã«ã®å ŽåãQAbstractListModelãã掟çã¯ã©ã¹ãäœæãããã®ãããªã¡ãœãããå®çŸ©ããå¿ èŠããããŸãã
- rowCountïŒïŒ-è¡æ°ãè¿ããŸãããã®å Žåãããã¯èŠçŽ ã®æ°ã§ãã
- dataïŒïŒ-èŠçŽ ã®ããŒã¿ãè¿ããŸãã
- roleNamesïŒïŒ-ããªã²ãŒãã§å©çšå¯èœãªããŒã«ã®ãªã¹ããè¿ããŸãã ããã©ã«ãã§ã¯ã衚瀺ãè£ é£Ÿãç·šéãtoolTipãstatusTipãããã³whatsThisã®åœ¹å²ãå®çŸ©ãããŠããŸãã Qtã®4çªç®ã®ããŒãžã§ã³ã§ã¯ããã®é¢æ°ããªãŒããŒã©ã€ããã代ããã«ãå¿ èŠãªããŒã«åãèšå®ããsetRoleNamesïŒïŒé¢æ°ãåŒã³åºãå¿ èŠããããŸããã
ããªã²ãŒãã®å©ããåããŠã¢ãã«ããŒã¿ãç·šéããäºå®ããªãå Žåã¯ãããã§ååã§ãã ç·šéããã¢ãã«ã«ã€ããŠã¯å°ãåŸã§æ€èšããŸãã
ããŒãã«ã¢ãã«ã®å Žåãåæ°ãè¿ãcolumnCountïŒïŒã¡ãœãããè¿œå ãããŸãã QMLããŒãã«ãã¥ãŒã¯æåã®åã®èŠçŽ ã䜿çšãã衚瀺ããããšããã®èŠçŽ ã®åœ¹å²ãåãšããŠåæ£ããŸãã ãããã£ãŠãQMLã®ããŒãã«ã¯åããªã¹ãã䜿çšããŠå®è£ ãããããŒãã«ã¢ãã«ã䜿çšããããšã¯ã»ãšãã©æå³ããããŸããã
ããªãŒæ§é ã®ã¢ãã«ãå¿ èŠãªå Žåã¯ãQAbstractItemModelã䜿çšããŸãã ãã®ã¢ãã«ã§ã¯ãããã«æ¬¡ã®æ©èœã決å®ããå¿ èŠããããŸãã
- parentïŒïŒ-èŠçŽ ã®èŠªã€ã³ããã¯ã¹ãè¿ããŸãã
- indexïŒïŒ-èŠçŽ ã®ã€ã³ããã¯ã¹ãè¿ããŸãã
Qtã¢ãã«ã§ã¯ãèŠçŽ ãžã®ã¢ã¯ã»ã¹ã¯ç¹å¥ãªã€ã³ããã¯ã¹-QModelIndexåã®ãªããžã§ã¯ããä»ããŠè¡ãããŸãã ãããã«ã¯ãè¡ãšåã®çªå·ã芪èŠçŽ ã®ã€ã³ããã¯ã¹ãããã³ããã€ãã®è¿œå ããŒã¿ãå«ãŸããŸãã ã¢ãã«ã®ã«ãŒãèŠçŽ ã«ç¡å¹ãªQModelIndexïŒïŒã€ã³ããã¯ã¹ããããŸãã ãããã£ãŠãåçŽãªãªã¹ããŸãã¯ããŒãã«ãããå Žåããã¹ãŠã®èŠçŽ ã«ã€ããŠã芪èŠçŽ ã¯ããã ãã«ãªããŸãã ããªãŒã®å ŽåãæäžäœèŠçŽ ã®ã¿ããã®ãããªèŠªãæã¡ãŸãã indexïŒïŒé¢æ°ã¯ã芪ã®ã€ã³ããã¯ã¹ãšèŠçŽ ã®è¡çªå·ãšåçªå·ãååŸããèŠçŽ ã®ã€ã³ããã¯ã¹ãè¿ãå¿ èŠããããŸãã ã€ã³ããã¯ã¹ã¯createIndexïŒïŒé¢æ°ã䜿çšããŠäœæãããŸãã
å®éããã¹ããå¿ èŠã«ãªããšå°é£ãå§ãŸãããã¹ãŠãéåžžã«ç°¡åã§ãã
äŸãšããŠããªã¹ãã¢ãã«ãèããŸãã ããŒã¿ã¯ãè¡ã®ãªã¹ããšåããªããžã§ã¯ãã«ä¿åãããŸãã addïŒïŒé¢æ°ãäœæããŸãããã®é¢æ°ã¯ãã¢ãã«ã«ãã1ã€ã®èŠçŽ ãè¿œå ããQMLããåŒã³åºããããã«ç¹å¥ãªãã¯ãQ_INVOKABLEã§ããŒã¯ããŸãã
ã¯ã©ã¹å®çŸ©ïŒ
#include <QAbstractListModel> #include <QStringList> class TestModel : public QAbstractListModel { Q_OBJECT public: enum Roles { ColorRole = Qt::UserRole + 1, TextRole }; TestModel(QObject *parent = 0); virtual int rowCount(const QModelIndex &parent) const; virtual QVariant data(const QModelIndex &index, int role) const; virtual QHash<int, QByteArray> roleNames() const; Q_INVOKABLE void add(); private: QStringList m_data; };
ColorRoleãšTextRoleã®2ã€ã®ããŒã«ãå®çŸ©ãããããã«Qt :: UserRoleãã倧ããå€ã䜿çšããŸããããã¯ãQtã®äºçŽå€ãçµäºããå Žæã§ãã ãããã£ãŠããŠãŒã¶ãŒããŒã«ã®å ŽåãQt :: UserRoleã§å§ãŸãå€ã䜿çšããå¿ èŠããããŸãã
ã¯ã©ã¹ã¡ãœããã®å®è£ ïŒ
TestModel::TestModel(QObject *parent): QAbstractListModel(parent) { m_data.append("old"); m_data.append("another old"); } int TestModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) { return 0; } return m_data.size(); } QVariant TestModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } switch (role) { case ColorRole: return QVariant(index.row() < 2 ? "orange" : "skyblue"); case TextRole: return m_data.at(index.row()); default: return QVariant(); } } QHash<int, QByteArray> TestModel::roleNames() const { QHash<int, QByteArray> roles = QAbstractListModel::roleNames(); roles[ColorRole] = "color"; roles[TextRole] = "text"; return roles; } void TestModel::add() { beginInsertRows(QModelIndex(), m_data.size(), m_data.size()); m_data.append("new"); endInsertRows(); m_data[0] = QString("Size: %1").arg(m_data.size()); QModelIndex index = createIndex(0, 0, static_cast<void *>(0)); emit dataChanged(index, index); }
QMLã¯æŽæ°å®æ°ã§ã¯ãªãæåååã䜿çšããŠããŒã«ã«ã¢ã¯ã»ã¹ãããããè²ãšããã¹ãã®ååãå®çŸ©ããŸãã è¿œå ããåã«ãç¹å¥ãªé¢æ°beginInsertRowsïŒïŒãåŒã³åºããŸããããã¯ãèŠçŽ ãæºåãããŠããããšãšããããè¿œå ãããå Žæããã¥ãŒãèªèã§ããããã«ãå¿ èŠãªä¿¡å·ãçºè¡ããŸãã 次ã«ãendInsertRowsïŒïŒé¢æ°ãåŒã³åºããŸãããã®é¢æ°ã¯ãã¢ãã«ã«èŠçŽ ãè¿œå ãããããšã瀺ãä¿¡å·ãå床éä¿¡ããŸãã ãã¹ãŠã®è¿œå ã¯ããã®æ¹æ³ã§ã©ããããå¿ èŠããããŸãã ã¢ã€ãã ãåé€ããã³ç§»åããããã®åæ§ã®æ©èœããããŸãã
addïŒïŒé¢æ°ã§ã¯ãæåã®èŠçŽ ã®ããã¹ããå€æŽããŠããªã¹ãå ã®èŠçŽ ã®æ°ã衚瀺ããŸãã ãã®åŸãã·ã°ãã«dataChangedïŒïŒãçºè¡ããŠããã¥ãŒã«ã€ããŠéç¥ããŸãã å€æŽãããããŒã¿ã®æåãšæåŸã®ã€ã³ããã¯ã¹ã®ãã©ã¡ãŒã¿ãŒã«ä¿¡å·ãæž¡ããŸãïŒåããã®ããããŸãïŒã ã€ã³ããã¯ã¹ã¯createIndexïŒïŒé¢æ°ã䜿çšããŠååŸãããŸãããã®é¢æ°ã¯ããã©ã¡ãŒã¿ãè¡ãåãããã³ãã©ã€ããŒãããŒã¿ãžã®ãã€ã³ã¿ã«æž¡ããŸãã éåžžãããŒã¿ãæã€ãªããžã§ã¯ããžã®ãã€ã³ã¿ãŒãåŸè ãšããŠäœ¿çšãããŸããããã®å ŽåãåçŽåããŠåžžã«NULLã䜿çšã§ããŸãã
QMLããã°ã©ã ãšããŠã2çªç®ã®äŸãå°ãããçŽããŸãã C ++-ã¢ãã«ã¯ãã©ã°ã€ã³ïŒãã©ã°ã€ã³ïŒãšããŠå®è£ ãããŸãã ãã¡ã€ã«ã®å é ã«ãã€ã³ããŒããè¿œå ããŸãã
import TestModel 1.0
ãã®ã¿ã€ãã®ãªããžã§ã¯ããäœæããã¢ãã«ãšããŠäœ¿çšããŸãã
TestModel { id: dataModel }
ããã°ã©ã ãéå§ããŠããã€ãã®èŠçŽ ãè¿œå ãããšã次ã®ãããªçµæãåŸãããŸãã
ã¢ãã«ããŒã¿ãç·šéããã«ã¯ãããªã²ãŒãã«æšæºã€ã³ã¿ãŒãã§ã€ã¹ããããããã䜿çšããã«ã¯ãã¢ãã«ã®setDataïŒïŒã¡ãœãããåå®çŸ©ããå¿ èŠããããŸãã QMLããQAbstractItemModelããŒã¿ãç·šéããæ©èœã¯Qt 5ã§ç»å ŽããŸããã
次ã®å®£èšãããããŒãã¡ã€ã«ã«è¿œå ããŸãã
virtual bool setData(const QModelIndex &index, const QVariant &value, int role); virtual Qt::ItemFlags flags(const QModelIndex &index) const;
ãããŠãå®çŸ©å®è£ ãã¡ã€ã«ã«ïŒ
bool TestModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) { return false; } switch (role) { case ColorRole: return false; // This property can not be set case TextRole: m_data[index.row()] = value.toString(); break; default: return false; } emit dataChanged(index, index, QVector<int>() << role); return true; } Qt::ItemFlags TestModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::ItemIsEnabled; return QAbstractListModel::flags(index) | Qt::ItemIsEditable; }
ã¢ãã«ãšåæ§ã®ã³ãŒãã䜿çšããŠãããªã²ãŒãããããã¹ãããããã£ãçŽæ¥ç·šéããæ©èœãè¿œå ããŸããã
model.text = "Some new text"
ãã®äŸã®ããªã²ãŒããç·šéããŠã次ã®èŠçŽ ãè¿œå ããŠã¿ãŸãããã
MouseArea { anchors.fill: parent onDoubleClicked: model.text = "Edited" }
ããã§ãèŠçŽ ãããã«ã¯ãªãã¯ãããšããã®ããã¹ãããç·šéæžã¿ãã«å€ãããŸãã
Qt :: ItemIsEditableãã©ã°ã¯Qt衚瀺ã«äœ¿çšãããã¢ã€ãã ãç·šéã§ããããšã瀺ããããflagsïŒïŒã¡ãœãããåå®çŸ©ããå¿ èŠããããŸãã çŸæç¹ã§ã¯ããã®ãã©ã°ã¯QMLã§ãã§ãã¯ãããŠããããã¢ãã«ã¯ã€ã³ã¹ããŒã«ãããŠããªããŠãç·šéå¯èœã§ãããç¡èŠããªãããšããå§ãããŸãã å°æ¥ã®ããŒãžã§ã³ã§ã¯ããã®ãã§ãã¯ãè¿œå ãããå¯èœæ§ããããŸãã
2. C ++-ãªã¹ã
ã¢ãã«ãšããŠãæååã®ãªã¹ããŸãã¯QObjectåã®ãªããžã§ã¯ãã䜿çšã§ããŸãã
QStringListåã®ããããã£ãæã€åçŽãªã¯ã©ã¹ãäœæããŠã¿ãŸãããã
#include <QObject> #include <QStringList> class TestModel : public QObject { Q_OBJECT Q_PROPERTY(QStringList data READ data CONSTANT) public: TestModel(QObject *parent = 0); QStringList data() const; }; TestModel::TestModel(QObject *parent): QObject(parent) { } QStringList TestModel::data() const { return QStringList() << "orange" << "skyblue"; }
å°ãããçŽããæåã®äŸã䜿çšããŸãã ã¢ãã«ãªããžã§ã¯ãã®ã€ã³ããŒããšäœæã¯ãåã®äŸãšãŸã£ããåãã§ãã ãã ãããªããžã§ã¯ãèªäœã®ä»£ããã«ããã®ããããã£ãã¢ãã«ãšããŠäœ¿çšãããŸãã
model: dataModel.data
ããã¹ãã¯èŠçŽ ã®ã€ã³ããã¯ã¹ã䜿çšããŸãïŒ
text: model.index
ãã®ãããªãªã¹ãã¯ãJavaScripté åã®ããã«æ©èœããŸãã ãããã£ãŠãããã¯ååã¢ãã«ã§ãããèŠçŽ ã®è¿œå /åé€ã¯ãã¬ãŒã³ããŒã·ã§ã³ã«åœ±é¿ããŸããã
3. QQmlListProperty
ãã®ã¯ã©ã¹ã䜿çšãããšãC ++ãšQMLã®äž¡æ¹ã«å ¥åã§ãããªã¹ããäœæã§ããŸãã QMLã®å ¥åã¯ããªããžã§ã¯ãã®äœææã«éçã«å®è¡ãããŸãïŒListModelã§è¡ãããŸãïŒã C ++ã§ã¯ãèŠçŽ ãè¿œå /åé€ããããšãã§ããŸãããã®ãããç¹å¥ãªã¡ãœãããäœæããŠQ_INVOKABLEãã¯ãã§ããŒã¯ãããšãQMLããããããè¡ãããšãã§ããŸãã
ãã®ã¿ã€ãã®ãªã¹ãã«ã¯ãã¿ã€ãQObjectã®ãªããžã§ã¯ããšãããã掟çããã¿ã€ããä¿åã§ããŸãã åã§ã¯ã䜿çšããããã¹ãŠã®ããããã£ãå®çŸ©ãã䟡å€ããããŸãïŒQ_PROPERTYã䜿çšïŒã
ãã®ãããªãªããžã§ã¯ãã®äŸãèããŠã¿ãŸãããã
#include <QObject> class Element : public QObject { Q_OBJECT Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) public: explicit Element(QObject *parent = 0); QString color() const; void setColor(QString color); QString text() const; void setText(QString text); signals: void colorChanged(QString color); void textChanged(QString text); private: QString m_color; QString m_text; }; Element::Element(QObject *parent) : QObject(parent) { } QString Element::color() const { return m_color; } void Element::setColor(QString color) { if (m_color == color) { return; } m_color = color; emit colorChanged(m_color); } QString Element::text() const { return m_text; } void Element::setText(QString text) { if (m_text == text) { return; } m_text = text; emit textChanged(m_text); }
è²ãšããã¹ããã²ãã¿ãŒãã»ãã¿ãŒãããã³ãããã®éç¥æ©èœãšãã2ã€ã®ããããã£ãå«ãåçŽãªã¯ã©ã¹ãäœæããŸããã
ãã®ã¿ã€ãã®ãªããžã§ã¯ããQQmlListPropertyã§äœ¿çšããã«ã¯ããã®ã¿ã€ããQMLã§è¡šç€ºãããŠããå¿ èŠããããŸãã ãã®ããã«ã¯ãqmlRegisterTypeïŒïŒé¢æ°ã䜿çšããŠãã®ã¿ã€ããç»é²ããå¿ èŠããããŸãã ç§ã¯C ++ãã©ã°ã€ã³ã䜿çšããŠããã®ã§ããã®ã¿ã€ããã¢ãã«ãšãšãã«ç¹å¥ãªãã³ãã©ãŒã«ç»é²ããŸãã
void TestModelPlugin::registerTypes(const char *uri) { qmlRegisterType<TestModel>(uri, 1, 0, "TestModel"); qmlRegisterType<Element>(uri, 1, 0, "Element"); }
QQmlListPropertyã䜿çšããã«ã¯ããªããžã§ã¯ãã«QQmlListPropertyåã®ããããã£ãäœæããå¿
èŠããããŸããããã§ãTã¯ä¿åãããªããžã§ã¯ãã®åã§ãã ãã®å Žåãã¿ã€ãQQmlListPropertyã®ããããã£ããããŸãã
QQmlListPropertyã³ã³ã¹ãã©ã¯ã¿ãŒã¯ããªã¹ããæäœãããšãã«QMLãšã³ãžã³ãåŒã³åºãã¡ãœãããåŒæ°ãšããŠåãåããŸãã ãããã¯ãã¢ã€ãã ã®è¿œå ãšååŸãã¢ã€ãã ã®æ°ã®ååŸãããã³ãªã¹ãã®ã¯ãªã¢ã®ããã®ã¡ãœããã§ãã æåã®ãã®ã®ã¿ãå¿ é ã§ããããã¹ãŠãå®çŸ©ããæ¹ãè¯ãã§ãã
ãããã£ãŠãã¢ãã«ã®ã¯ã©ã¹ã³ãŒãïŒ
#include <QObject> #include <QQmlListProperty> class Element; class TestModel : public QObject { Q_OBJECT Q_PROPERTY(QQmlListProperty<Element> data READ data NOTIFY dataChanged) Q_CLASSINFO("DefaultProperty", "data") public: TestModel(QObject *parent = 0); QQmlListProperty<Element> data(); Q_INVOKABLE void add(); signals: void dataChanged(); private: static void appendData(QQmlListProperty<Element> *list, Element *value); static int countData(QQmlListProperty<Element> *list); static Element *atData(QQmlListProperty<Element> *list, int index); static void clearData(QQmlListProperty<Element> *list); QList<Element*> m_data; }; TestModel::TestModel(QObject *parent): QObject(parent) { Element *element = new Element(this); element->setProperty("color", "lightgreen"); element->setProperty("text", "eldest"); m_data << element; } QQmlListProperty<Element> TestModel::data() { return QQmlListProperty<Element>(static_cast<QObject *>(this), static_cast<void *>(&m_data), &TestModel::appendData, &TestModel::countData, &TestModel::atData, &TestModel::clearData); } void TestModel::add() { Element *element = new Element(this); element->setProperty("color", "skyblue"); element->setProperty("text", "new"); m_data.append(element); emit dataChanged(); } void TestModel::appendData(QQmlListProperty<Element> *list, Element *value) { QList<Element*> *data = static_cast<QList<Element*> *>(list->data); data->append(value); } int TestModel::countData(QQmlListProperty<Element> *list) { QList<Element*> *data = static_cast<QList<Element*> *>(list->data); return data->size(); } Element *TestModel::atData(QQmlListProperty<Element> *list, int index) { QList<Element*> *data = static_cast<QList<Element*> *>(list->data); return data->at(index); } void TestModel::clearData(QQmlListProperty<Element> *list) { QList<Element*> *data = static_cast<QList<Element*> *>(list->data); qDeleteAll(data->begin(), data->end()); data->clear(); }
QAbstractItemModelã®äŸã®ããã«ãèŠçŽ ãè¿œå ããããã®addïŒïŒã¡ãœããããããèŠçŽ ãã³ã³ã¹ãã©ã¯ã¿ãŒã«è¿œå ãããŸãã
ã¿ã€ãQQmlListPropertyã®ãªããžã§ã¯ãã¯ãdataïŒïŒã¡ãœããã§äœæãããŸãã ã³ã³ã¹ãã©ã¯ã¿ãŒã§ã¯ã芪ïŒQObjectïŒããã©ã€ããŒãããŒã¿ãžã®ãã€ã³ã¿ãŒãåãåããŸããããã¯ããªã¹ããšé¢æ°èªäœãæäœããããã®é¢æ°ã§äœ¿çšã§ããŸãã ãã¹ãŠã®é¢æ°ã§ãæåã®åŒæ°ã¯ãããŒã¿ããããã£ã«ãã©ã€ããŒãããŒã¿ãæã€QQmlListPropertyåã®ãªããžã§ã¯ããžã®ãã€ã³ã¿ãŒã§ãã ã¿ã€ãElementã®ãªããžã§ã¯ããå®éã«ä¿åããããªã¹ããããã«çœ®ããŸãã
æäœäžã«ãªããžã§ã¯ããè¿œå /åé€ãããšãã«ããã¥ãŒãã¢ãã«ã®å€æŽã«é¢ããæ å ±ãåãåãããã«ãããŒã¿ããããã£ã®ä¿¡å·ãå¿ èŠã§ãã ãã®ãããªä¿¡å·ã®åŸããã£ã¹ãã¬ã€ã¯ã¢ãã«å šäœãåèªã¿åãããŸãã
ãã®ã¢ãã«ã瀺ãããã«ããããã«ããçŽãã2çªç®ã®äŸãåãäžããŸãã
C ++ãæ¥ç¶ããŸã-ãã©ã°ã€ã³ïŒ
import ListProperty_Plugin 1.0 : TestModel { id: dataModel data: [ Element { color: "orange" text: "old" }, Element { color: "lightgray" text: "another old" } ] }
ããŒã¿ããããã£ã¯ãéåžžã®ãªã¹ããšããŠå®çŸ©ãããŸãã Elementã¿ã€ããç»é²ããããããã®ãããªãªããžã§ã¯ããQMLã§äœæã§ããããã«ãªããŸããã ããã§ã®ããŒã¿é åã®èŠçŽ ã®å®çŸ©ã¯ããã§ã«ååšããèŠçŽ ã眮ãæãããã®ã§ã¯ãªãããšã«æ³šæããŠãã ããã ãããã®èŠçŽ ã¯ãTestModelã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã§å®çŸ©ãããèŠçŽ ã«è¿œå ãããŸãã
ã¢ãã«èªäœã¯TestModelåã®ãªããžã§ã¯ãã§ã¯ãªããåãããŒã¿ããããã£ã§ãã
model: dataModel.data
ããªã²ãŒãã®ããŒã¿ã«ã¯ãmodelDataããã¢ã¯ã»ã¹ã§ããŸãã
color: modelData.color
ãããŠ
text: modelData.text
èŠçŽ ã¯dataããããã£ã«éçã«ã®ã¿è¿œå ã§ããããããã®ããã«äœæããaddïŒïŒé¢æ°ã䜿çšããŸãã
onClicked: dataModel.add()
ãã®çµæã次ã®ãããªçµæãåŸãããŸãã
TestModelã¯ã©ã¹ã§ã¯ãããŒã¿ãããã©ã«ãããããã£ãšããŠæå®ããŸããïŒQ_CLASSINFOãã£ã¬ã¯ãã£ãã䜿çšïŒã ããã«ãããTestModelãªããžã§ã¯ãã§Elementãªããžã§ã¯ããçŽæ¥å®çŸ©ããæ©äŒãäžãããããããèªäœãç®çã®ããããã£ã«è¿œå ãããŸãã ãããã£ãŠãã¢ãã«ã®å®çŸ©ãåçŽåãã次ã®ããã«æžãæããããšãã§ããŸãã
TestModel { id: dataModel Element { color: "orange" text: "old" } Element { color: "lightgray" text: "another old" } }
ãããã£ãŠãQQmlListPropertyã䜿çšãããšãQAbstractItemModelã¯ã©ã¹ã䜿çšããã«ã¢ã¯ãã£ãã¢ãã«ãå®è£ ã§ããŸãã 倧éã®ããŒã¿ãäºæãããŠããããé »ç¹ã«å€æŽãããã¹ãã§ã¯ãªãå Žåããã®ãããªã¢ãã«ã¯éåžžã«é©ããŠããŸãã
ãŸãšã
ã¢ãã«éçºã¯ãQMLããã°ã©ãã³ã°ã ãã§ãªããããã°ã©ãã³ã°å šäœã®éèŠãªéšåã§ãã ãã¬ããã»ãã«ãã¯ã¹ãèšã£ãããã«ãããããŒãã£ãŒãã衚瀺ããããŒãã«ãé衚瀺ã«ãããšãå°æããããŒãã«ã衚瀺ãããããŒãã£ãŒããå¿ èŠãšããªãå¯èœæ§ãé«ããªããŸããããã¯æããã§ããã ãã®ããŒã¿ã¯ããã°ã©ãã³ã°ã®äžå¿çãªããŒãã§ãã ããŒã¿æ§é ã®èšèšãšã¢ã¯ã»ã¹ã¯éèŠãªã¿ã¹ã¯ã§ãããããã°ã©ã ã®ã¢ãŒããã¯ãã£ã倧ãã決å®ããŸãã
ãã®éšåãšåã®éšåã§èª¬æããããŒã«ãç解ãããšãããŒã¿ãæãé©åãªæ¹æ³ã§æŽçããããŒã¿ãšããã°ã©ã èªäœãæŽçããã®ã«åœ¹ç«ã¡ãŸãã Model-Viewã®æŠå¿µã¯QMLã®åºæ¬æŠå¿µã®1ã€ã§ããããããããã®ããŒã«ã§ååã§ãã
ã¢ãã«ãäœæããããŸããŸãªæ¹æ³ãæ€èšããŸããã ç§èªèº«ã®çµéšãããæããã䜿çšãããã®ã¯QAbstractItemModelãListModelãããã³JavaScripté åã§ãã ã ããããŸã第äžã«ã泚æãæãããšããå§ãããŸãã