2015-09-14 21:38:02 +00:00
|
|
|
import NimQml, Tables
|
|
|
|
|
2016-03-22 22:06:31 +00:00
|
|
|
type
|
|
|
|
RoleNames {.pure.} = enum
|
|
|
|
Name = UserRole + 1,
|
|
|
|
|
2015-09-14 21:38:02 +00:00
|
|
|
QtObject:
|
|
|
|
type
|
|
|
|
MyListModel* = ref object of QAbstractListModel
|
|
|
|
names*: seq[string]
|
|
|
|
|
|
|
|
proc delete(self: MyListModel) =
|
2016-03-22 22:06:31 +00:00
|
|
|
self.QAbstractListModel.delete
|
|
|
|
|
|
|
|
proc setup(self: MyListModel) =
|
|
|
|
self.QAbstractListModel.setup
|
2015-09-14 21:38:02 +00:00
|
|
|
|
|
|
|
proc newMyListModel*(): MyListModel =
|
|
|
|
new(result, delete)
|
|
|
|
result.names = @["John", "Max", "Paul", "Anna"]
|
2016-03-22 22:06:31 +00:00
|
|
|
result.setup
|
2015-09-14 21:38:02 +00:00
|
|
|
|
2016-03-22 22:06:31 +00:00
|
|
|
method rowCount(self: MyListModel, index: QModelIndex = nil): int =
|
2015-09-14 21:38:02 +00:00
|
|
|
return self.names.len
|
|
|
|
|
2016-03-22 22:06:31 +00:00
|
|
|
method data(self: MyListModel, index: QModelIndex, role: int): QVariant =
|
2015-09-14 21:38:02 +00:00
|
|
|
if not index.isValid:
|
|
|
|
return
|
|
|
|
if index.row < 0 or index.row >= self.names.len:
|
|
|
|
return
|
2016-03-22 22:06:31 +00:00
|
|
|
return newQVariant(self.names[index.row])
|
2015-09-14 21:38:02 +00:00
|
|
|
|
2016-03-22 22:06:31 +00:00
|
|
|
method roleNames(self: MyListModel): Table[int, string] =
|
|
|
|
{ RoleNames.Name.int:"name"}.toTable
|