我有一个使用 QAbstractListModel 子类作为模型的 QML ListView。
ListView {
id: myListView
x: 208
y: 19
width: 110
height: 160
delegate: myListDelegate {}
model: MyListModel
opacity: 0
}
该模型是 MyListItem
s 的列表。
class MyListModel : public QAbstractListModel
{
Q_OBJECT
public:
enum MyRoles {
HeadingRole = Qt::UserRole + 1,
DescriptionRole,
QuantityRole
};
explicit MyListModel(QObject *parent = 0);
void addMyListItem(const MyListItem &item);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
void dropList();
private:
QList<MyListItem> m_list;
};
在代表中,我有一个鼠标区域。
如何拦截鼠标区域的点击并从我的 QList 模型 中选择 MyListItem
并将其发送到应用程序的 C++ 部分中的某个位置?
原文由 Zhigalin - Reinstate CMs 发布,翻译遵循 CC BY-SA 4.0 许可协议
您还可以在委托中使用
index
属性来操作数据。您只需使用模型上的 index 方法将 QML 索引转换为QModelIndex
。这是一个简单的示例,每次单击列表项时,我们将显示值更改为字符串“3”。除了委托中的
index
属性之外,委托中的所有默认角色名称都可用。例如,我之前使用decoration
角色来设置我的Rectangle
委托的color
属性。有关更多信息,请参阅 此列表。另请参阅 Mitch Curtis 建议使用 qmlRegisterUncreatableType 注册用户枚举的 链接。