/
protected_test.lua
53 lines (42 loc) · 1.02 KB
/
protected_test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require "qtcore"
require "qtgui"
A = QApplication(1, {'Protected test'})
-- We will implement our custom model
M = QAbstractListModel()
-- stored in the environment table of the userdata
M.luaData = {'Hello', 'World'}
-- these are implemented virtual methods
function M:rowCount()
return #self.luaData
end
local empty = QVariant()
function M:data(index, role)
if role == Qt.ItemDataRole.DisplayRole then
local row = index:row()
return QVariant(self.luaData[row + 1])
end
return empty
end
-- this is a custom helper function
function M:addAnotherString(str)
table.insert(self.luaData, str)
local row = #self.luaData - 1
local index = self:createIndex(row, 0, 0)
self:dataChanged(index, index)
end
-- some simple layout - list and a button
MW = QWidget()
W = QListView()
W:setModel(M)
B = QPushButton('Add Lua data')
local counter = 1
B:connect('2clicked()', function()
M:addAnotherString('Added text ' .. counter)
counter = counter + 1
end)
L = QVBoxLayout()
L:addWidget(W)
L:addWidget(B)
MW:setLayout(L)
MW:show()
A.exec()