게시 된 답변은 게시 된 답변과 약간 다릅니다 , 나는 명확한 대답을 쓰는 것이 가장 좋을 것이라고 생각했다.
의 핵심은 QAbstractItemModel
을 하위 클래스했다 (또는 더 정확하게 ..ListModel
그래서 당신은 및 QML ++ C의 행/열을 처리 할 필요가 없습니다).
이런 식으로 일을뿐만 아니라, 당신은 단순히 변경 모델, 예를 들면했을 경우에 반드시
QQuickItem *mainform = view->rootObject();
QQuickItem *grid = (QQuickItem *)mainform->findChild<QObject*>("GridView object name");
ItemModel itemmodel;
itemmodel.setItems(objlist):
QQmlProperty::write(grid, "model", QVariant::fromValue(&itemmodel));
또한 QML을 notifys와 속성으로 모델을 설정할 수 있습니다 항목이 삭제되었습니다.
// itemmodel.h
#include <QAbstractListModel>
#include <item.h>
class ItemModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit ItemModel(QObject *parent = 0);
QHash<int, QByteArray> roleNames() const;
public slots:
void setItems(QList<Item *> items);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex & parent = QModelIndex()) const;
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
private:
QList<Item *> items;
};
// itemmodel.cpp
#include "itemmodel.h"
ItemModel::ItemModel(QObject *parent) : QAbstractListModel(parent)
{
}
// Column Names have to match all the Q_PROPERTYs defined in Item
const char* COLUMN_NAMES[] = {
"property1",
"property2",
"...",
NULL
};
QHash<int, QByteArray> makeRoleNames()
{
int idx = 0;
QHash<int, QByteArray> roleNames;
while(COLUMN_NAMES[idx])
roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx++];
return roleNames;
}
QHash<int, QByteArray> ItemModel::roleNames() const
{
static const QHash<int, QByteArray> roleNames = makeRoleNames();
return roleNames;
}
void ItemModel::setItems(QList<Item *> items)
{
this->items = items;
}
int ItemModel::rowCount(const QModelIndex & /* parent */) const
{
return items.count();
}
bool ItemModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginRemoveRows(QModelIndex(), row, row + count - 1);
while (count--) delete items.takeAt(row);
// example for custom deletion:
// items.takeAt(row)->removeFromRoot();
endRemoveRows();
return true;
}
QVariant ItemModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (index.row() >= items.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole) {
return QVariant::fromValue(this->items.at(index.row()));
}
if (role > Qt::UserRole)
return this->items.at(index.row())->property(makeRoleNames()[role]);
}
출처 :
다음
내 ItemModel이다 (당신은 제대로하지만 변경 사항을 처리 할 수있는 예에
removeRows()
를 볼 수 있습니다)
- [1] Qt 설명서 페이지 QML
- [2] QAbstractItemModel의 참조 QAbstractItemModel 서브 클래스를 생성
- [3] QAbstractItemModel 서브 클래 싱 가이드
- QAbstractListModel 서브 클래스
- [5] 작업과 [4] 포럼 포스트 (거의,
data()
약간 변경) QAbstractItemModel의 구현이있는 내가 데려 rolenames 함수
'QQmlProperty :: wr을 사용하여 모델 속성을 설정하는 방법 ite' 대신'ctxt-> setContextProperty'를 사용하거나 전역 컨텍스트 변수를 사용하지 않고 GridView의 모델을 설정하는 방법을 말합니다 –
C++의 모델을 _setting_하는 대신 GridView에서 빈 모델을 정의하는 옵션이 아닙니까? 및 C++에서 _update_ QAbstractItemModel 함수를 사용하여 모델? 이렇게하면 QML이 GridView를 업데이트 할 수 있도록 해당 신호가 트리거됩니다. –
그게 _does_ 소리가 훨씬 더 좋아요, 특히 내 모델에서 항목을 제거하려고 할 때 문제가 생기기 때문에 더욱 그렇습니다. 그 때 정확히 어떻게 할 것입니까? 나는 C++/QML 통합에 대한 충분한 문서를 찾는 것이 매우 힘들다는 것을 알았다. –