display values from a list, and support changing that list from nim

This commit is contained in:
Iuri Matias 2020-05-08 18:04:53 -04:00
parent a0580893c4
commit 41dd96e9c9
5 changed files with 65 additions and 12 deletions

View File

@ -7,7 +7,7 @@ ApplicationWindow {
id: applicationWindow id: applicationWindow
width: 1024 width: 1024
height: 768 height: 768
title: "JSON RPC Caller" title: "Nim Status Client"
visible: true visible: true
RowLayout { RowLayout {
@ -237,6 +237,17 @@ ApplicationWindow {
font.pixelSize: 28 font.pixelSize: 28
} }
} }
Component {
id: chatViewDelegate
Label { text: "Name:" + name }
}
ListView {
id: listView
anchors.fill: parent
model: chatsModel
delegate: chatViewDelegate
}
} }
Item { Item {
@ -292,6 +303,6 @@ ApplicationWindow {
/*##^## /*##^##
Designer { Designer {
D{i:9;anchors_height:40;anchors_width:40}D{i:19;anchors_y:0}D{i:23;anchors_height:100;anchors_width:100} D{i:9;anchors_height:40;anchors_width:40}
} }
##^##*/ ##^##*/

View File

@ -20,6 +20,7 @@ QtObject:
app: QApplication app: QApplication
callResult: string callResult: string
accountResult: string accountResult: string
# chats: seq[ChatView]
# Constructor # Constructor
proc newApplicationLogic*(app: QApplication): ApplicationLogic = proc newApplicationLogic*(app: QApplication): ApplicationLogic =
@ -41,17 +42,13 @@ QtObject:
echo appState.title echo appState.title
appState.subscribe(proc () = appState.subscribe(proc () =
echo "1nd subscriber got a new update!" echo "1nd subscriber got a new update!"
for channel in appState.channels:
echo channel.name
) )
appState.addChannel("test") appState.addChannel("test")
appState.subscribe(proc () =
echo "2nd subscriber got a new update!"
)
appState.addChannel("test2") appState.addChannel("test2")
for channel in appState.channels: # result.chats.add(ChatView(name: "test"))
echo channel.name
# ¯\_(ツ)_/¯ dunno what is this # ¯\_(ツ)_/¯ dunno what is this
proc setup(self: ApplicationLogic) = proc setup(self: ApplicationLogic) =

38
src/chats.nim Normal file
View File

@ -0,0 +1,38 @@
import NimQml
import Tables
type
RoleNames {.pure.} = enum
Name = UserRole + 1,
QtObject:
type
ChatsModel* = ref object of QAbstractListModel
names*: seq[string]
proc delete(self: ChatsModel) =
self.QAbstractListModel.delete
proc setup(self: ChatsModel) =
self.QAbstractListModel.setup
proc newChatsModel*(): ChatsModel =
new(result, delete)
result.names = @["test", "test2"]
result.setup
proc addNameTolist*(self: ChatsModel, name: string) =
self.names.add(name)
method rowCount(self: ChatsModel, index: QModelIndex = nil): int =
return self.names.len
method data(self: ChatsModel, index: QModelIndex, role: int): QVariant =
if not index.isValid:
return
if index.row < 0 or index.row >= self.names.len:
return
return newQVariant(self.names[index.row])
method roleNames(self: ChatsModel): Table[int, string] =
{ RoleNames.Name.int:"name"}.toTable

View File

@ -1,5 +1,6 @@
import NimQml import NimQml
import applicationLogic import applicationLogic
import chats
proc mainProc() = proc mainProc() =
@ -11,6 +12,8 @@ proc mainProc() =
var app = newQApplication() var app = newQApplication()
defer: app.delete() # Defer will run this just before mainProc() function ends defer: app.delete() # Defer will run this just before mainProc() function ends
var chatsModel = newChatsModel();
defer: chatsModel.delete
var engine = newQQmlApplicationEngine() var engine = newQQmlApplicationEngine()
defer: engine.delete() defer: engine.delete()
@ -22,7 +25,12 @@ proc mainProc() =
let logicVariant = newQVariant(logic) let logicVariant = newQVariant(logic)
defer: logicVariant.delete defer: logicVariant.delete
let chatsVariant = newQVariant(chatsModel)
defer: chatsVariant.delete
chatsModel.addNameTolist("hello")
engine.setRootContextProperty("logic", logicVariant) engine.setRootContextProperty("logic", logicVariant)
engine.setRootContextProperty("chatsModel", chatsVariant)
engine.load("main.qml") engine.load("main.qml")
# Qt main event loop is entered here # Qt main event loop is entered here

View File

@ -1,5 +1,4 @@
type Channel = object type Channel = object
name*: string name*: string
type type