我有一个 QML / C++ 问题。如何在视图委托之外使用和编辑 QAbstractListModel 的元素?
我创建了一个我尝试做的玩具示例。我觉得我对 QML 中的 MVC 缺少一些理解。
在 c++ 方面,我有一个“联系人”类,该类源自QObject
存储我的所有联系人数据、这些联系人的列表 (ContactList) 以及基于此列表的 aQAbstractListModel(ContactListModel)。
在 QML 方面,我想要 2 个单独的组件。在左侧QListView
显示联系人列表,此处仅使用联系人类的一部分(例如姓名)。在右侧,我有一个显示当前联系人(在列表视图中选择的那个)的另一个组件,该组件还应该允许编辑联系人的内容,例如名称/描述等。并相应地更新列表视图。
C++:联系人:
class Contact : public QObject{
Q_OBJECT
Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged)
public:
explicit Contact(QObject* parent=nullptr,
QString i_name =QString(),
QString i_desc =QString(),
bool i_checked = false);
QString description() const;
void setDescription(QString desc);
QString name;
bool checked;
signals:
void descriptionChanged();
private:
QString m_description;
};
联系人列表:
class ContactList : public QObject
{
Q_OBJECT
Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex )
Q_PROPERTY(Contact* currentItem READ currentContact WRITE setCurrentContact )
public:
explicit ContactList(QObject *parent = nullptr);
QVector<Contact*> contacts() const;
bool setItemAt(int index, Contact* item);
int currentIndex() const;
void setCurrentIndex(int index);
Contact* currentContact() const;
void setCurrentContact(Contact* contact);
signals:
void preItemAppended();
void postItemAppended();
void preItemRemoved(int index);
void postItemRemoved();
public slots:
void appendItem();
void removeItem(int index);
private:
QVector<Contact*> m_contacts;
int m_currentIndex;
Contact* m_currentContact;
};
联系人列表型号:
class ContactListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(ContactList* list READ list WRITE setList )
public:
explicit ContactListModel(QObject *parent = nullptr);
enum {
CheckRole = Qt::UserRole,
NameRole,
DescriptionRole
};
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual QHash<int, QByteArray> roleNames() const override;
ContactList* list() const;
void setList(ContactList *list);
private:
ContactList *m_list;
};
在 QML 方面,我有我的 listView
Layout.fillWidth: true
Layout.fillHeight: true
id: contacts_list_view
clip: true
model: ContactModel {
id: _contactmodel
list: contactList
}
delegate:
RowLayout {
CheckBox {
checked: model.checked
onCheckStateChanged: {
console.log(" current Index before " + contacts_list_view.currentIndex)
contacts_list_view.currentIndex = index;
console.log(" current Index after " + contacts_list_view.currentIndex)
contactList.currentIndex = index
}
}
TextField {
text: model.name
}
}
}
和我的其他组件应该显示和编辑来自单个联系人的数据:
ColumnLayout{
id: contact_sheet
Layout.fillWidth: true
Layout.preferredWidth: 800
TextInput{
id: _contact_description
Layout.fillWidth: true
Layout.preferredHeight: 100
text: "Contact Description "
}
TextField {
text: contactList.currentContact.description
}
}
我已经尝试了几件事,但我不明白我应该如何编辑单个联系人并在发生更改时通知这两个项目(当前索引或联系信息)。