populate listview in qml from code

This commit is contained in:
Dan Ballard 2018-10-16 21:18:04 -07:00
parent 2ab81679f8
commit 8409b2bef4
7 changed files with 138 additions and 20 deletions

92
identities.go Normal file
View File

@ -0,0 +1,92 @@
package main
import (
"github.com/therecipe/qt/core"
)
func init() { QIdentityListModel_QmlRegisterType2("CustomQmlTypes", 1, 0, "QIdentityListModel") }
const (
Name = int(core.Qt__UserRole) + 1<<iota
Onion
Pcolor
)
type IdentityListItem struct {
name, onion, pcolor string
}
type QIdentityListModel struct {
core.QAbstractListModel
_ func() `constructor:"init"`
_ func() `signal:"remove,auto"`
_ func(obj []*core.QVariant) `signal:"add,auto"`
_ func(name string, onion string, pcolor string) `signal:"edit,auto"`
modelData []IdentityListItem
}
func (m *QIdentityListModel) init() {
m.modelData = []IdentityListItem{}
m.ConnectRoleNames(m.roleNames)
m.ConnectRowCount(m.rowCount)
m.ConnectData(m.data)
}
func (m *QIdentityListModel) roleNames() map[int]*core.QByteArray {
return map[int]*core.QByteArray{
Name: core.NewQByteArray2("Name", -1),
Onion: core.NewQByteArray2("Onion", -1),
Pcolor: core.NewQByteArray2("Pcolor", -1),
}
}
func (m *QIdentityListModel) rowCount(*core.QModelIndex) int {
return len(m.modelData)
}
func (m *QIdentityListModel) data(index *core.QModelIndex, role int) *core.QVariant {
item := m.modelData[index.Row()]
switch role {
case Name:
return core.NewQVariant14(item.name)
case Onion:
return core.NewQVariant14(item.onion)
case Pcolor:
return core.NewQVariant14(item.pcolor)
}
return core.NewQVariant()
}
func (m *QIdentityListModel) remove() {
if len(m.modelData) == 0 {
return
}
m.BeginRemoveRows(core.NewQModelIndex(), len(m.modelData)-1, len(m.modelData)-1)
m.modelData = m.modelData[:len(m.modelData)-1]
m.EndRemoveRows()
}
func (m *QIdentityListModel) add(item []*core.QVariant) {
m.BeginInsertRows(core.NewQModelIndex(), len(m.modelData), len(m.modelData))
m.modelData = append(m.modelData, IdentityListItem{ item[0].ToString(), item[1].ToString(), item[2].ToString()})
m.EndInsertRows()
}
func (m *QIdentityListModel) AddItem(i IdentityListItem) {
m.BeginInsertRows(core.NewQModelIndex(), len(m.modelData), len(m.modelData))
m.modelData = append(m.modelData, i)
m.EndInsertRows()
}
func (m *QIdentityListModel) edit(name string, onion string, pcolor string) {
if len(m.modelData) == 0 {
return
}
m.modelData[len(m.modelData)-1] = IdentityListItem{name, onion, pcolor}
m.DataChanged(m.Index(len(m.modelData)-1, 0, core.NewQModelIndex()), m.Index(len(m.modelData)-1, 1, core.NewQModelIndex()), []int{Name, Onion, Pcolor})
}

17
main.go
View File

@ -23,6 +23,8 @@ type QmlCwtchApp struct {
_ int `property:"torStatusProgress"` _ int `property:"torStatusProgress"`
_ string `property:"torStatusSummary"` _ string `property:"torStatusSummary"`
//_ QIdentityListModel `property:"identities"`
_ func() `constructor:"init"` _ func() `constructor:"init"`
} }
@ -49,8 +51,8 @@ func InitCwtch(engine *qml.QQmlApplicationEngine, qCwtchApp *QmlCwtchApp) error
go func() { go func() {
for { for {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
status, _ := asaur.GetInfo("localhost:9051", "tcp4", "", "status/bootstrap-phase") rawStatus, _ := asaur.GetInfo("localhost:9051", "tcp4", "", "status/bootstrap-phase")
status := asaur.ParseBootstrapPhase(rawStatus)
progress, _ := strconv.Atoi(status["PROGRESS"]) progress, _ := strconv.Atoi(status["PROGRESS"])
qCwtchApp.SetTorStatusProgress(progress) qCwtchApp.SetTorStatusProgress(progress)
@ -68,6 +70,8 @@ func InitCwtch(engine *qml.QQmlApplicationEngine, qCwtchApp *QmlCwtchApp) error
log.Fatalf("Error initializing application: %v", err) log.Fatalf("Error initializing application: %v", err)
} }
<-doneChan <-doneChan
return nil return nil
@ -89,10 +93,19 @@ func main() {
quickcontrols2.QQuickStyle_SetStyle("Material") quickcontrols2.QQuickStyle_SetStyle("Material")
var qCwtchApp = NewQmlCwtchApp(nil) var qCwtchApp = NewQmlCwtchApp(nil)
var identityList = NewQIdentityListModel(nil)
alice := IdentityListItem{"alice", "1234567890abcdef", "red"}
identityList.AddItem(alice)
//qCwtchApp.SetIdentities(append(qCwtchApp.Identities(), alice))*/
// create the qml application engine // create the qml application engine
engine := qml.NewQQmlApplicationEngine(nil) engine := qml.NewQQmlApplicationEngine(nil)
//engine.QmlRegisterType()
engine.RootContext().SetContextProperty("cwtchApp", qCwtchApp) engine.RootContext().SetContextProperty("cwtchApp", qCwtchApp)
engine.RootContext().SetContextProperty("identityList", identityList)
// load the embeeded qml file // load the embeeded qml file
// created by either qtrcc or qtdeploy // created by either qtrcc or qtdeploy

View File

@ -4,7 +4,6 @@
<file>qml/LoadingWindow.qml</file> <file>qml/LoadingWindow.qml</file>
<file>qml/MainWindow.qml</file> <file>qml/MainWindow.qml</file>
<file>qml/ProfilesColumn.qml</file> <file>qml/ProfilesColumn.qml</file>
<file>assets/open_privacy_logo_white_bg.png</file>
<file>qml/main.qml</file> <file>qml/main.qml</file>
<file>assets/cwtch-logo.png</file> <file>assets/cwtch-logo.png</file>
</qresource> </qresource>

View File

@ -1,10 +1,6 @@
import QtQuick 2.0 import QtQuick 2.0
Rectangle { Rectangle {
height: parent.height
width: 300
anchors.top: parent.top
color: "#4a3458" color: "#4a3458"
property alias model: list.model property alias model: list.model

View File

@ -1,5 +1,6 @@
import QtQuick 2.9 import QtQuick 2.9
import QtQuick.Window 2.2 import QtQuick.Window 2.2
import CustomQmlTypes 1.0
Window { Window {
//visible: false //visible: false
@ -8,25 +9,36 @@ Window {
height: 800 height: 800
title: qsTr("Bulletin") title: qsTr("Bulletin")
property alias model: profilesColumn.model
ProfilesColumn { ProfilesColumn {
id: profilesColumn id: profilesColumn
model: profiles model: identityList
anchors.top: parent.top
anchors.left: parent.left
height: parent.height
width: 100
} }
GroupsColumn { GroupsColumn {
id: groupsColumn id: groupsColumn
anchors.left: profilesColumn.right
model: groups model: groups
anchors.top: parent.top
anchors.left: profilesColumn.right
height: parent.height
width: 250
} }
ListModel { /*ListModel {
id: profiles id: profiles
ListElement {name: "alice"; onion: "710829b408e8b368273cbc20b07f1c0d"; pcolor: "#ff0000"} ListElement {name: "alice"; onion: "710829b408e8b368273cbc20b07f1c0d"; pcolor: "#ff0000"}
ListElement {name: "bob"; onion: "a219b9740fc76367833cbc20b07d1cee"; pcolor: "#00ff00" } ListElement {name: "bob"; onion: "a219b9740fc76367833cbc20b07d1cee"; pcolor: "#00ff00" }
ListElement {name: "carol"; onion: "930829b408e8b364563cbc20b07a6560"; pcolor: "#0000ff" } ListElement {name: "carol"; onion: "930829b408e8b364563cbc20b07a6560"; pcolor: "#0000ff" }
ListElement {name: "+"; onion: "930829b408e8b364563cbc20b07a6560"; pcolor: "#a0a0a0" } ListElement {name: "+"; onion: "930829b408e8b364563cbc20b07a6560"; pcolor: "#a0a0a0" }
} }*/
ListModel { ListModel {
id: groups id: groups
@ -39,7 +51,13 @@ Window {
ListElement {type: "bulletin"; title: "Game Discussions"; groupid: "cc45892408123879273ec2a435cc4234"} ListElement {type: "bulletin"; title: "Game Discussions"; groupid: "cc45892408123879273ec2a435cc4234"}
} }
Connections {
target: cwtchApp
onIdentitiesChanged: { /*ProfilesColumn.model = cwtchApp.identities ;
profilesColumn.update();*/
}
}
} }

View File

@ -1,17 +1,14 @@
import QtQuick 2.0 import QtQuick 2.0
import CustomQmlTypes 1.0
Rectangle { Rectangle {
color: "#381F47" color: "#381F47"
height: parent.height
width: 100
anchors.top: parent.top
anchors.left: parent.left
property alias model: list.model property alias model: list.model
Text { color: "white"; text: "length: " + cwtchApp.identities.length }
ListView { ListView {
id: list id: list
@ -29,8 +26,8 @@ Rectangle {
width: 100 width: 100
Column { Column {
Rectangle { width: 80; height: 80; color: pcolor } Rectangle { width: 80; height: 80; color: Pcolor }
Text { color: "#a0a0a0"; text: name } Text { color: "#a0a0a0"; text: Name }
} }
} }
} }

View File

@ -2,6 +2,7 @@ import QtQuick 2.0
import QtQuick.Controls 1.0 import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0 import QtQuick.Layouts 1.0
import QtQuick.Window 2.0 import QtQuick.Window 2.0
import CustomQmlTypes 1.0
// Root non-graphical object providing window management and other logic. // Root non-graphical object providing window management and other logic.
QtObject { QtObject {
@ -11,6 +12,8 @@ QtObject {
onVisibleChanged: if (!visible) Qt.quit() onVisibleChanged: if (!visible) Qt.quit()
} }
property QIdentityListModel model: identityList
property LoadingWindow loadingWindow: LoadingWindow { visible: true } property LoadingWindow loadingWindow: LoadingWindow { visible: true }
property Timer timer: Timer { property Timer timer: Timer {
@ -29,7 +32,7 @@ QtObject {
target: cwtchApp target: cwtchApp
onTorStatusProgressChanged: { if (cwtchApp.torStatusProgress === 100) { onTorStatusProgressChanged: { if (cwtchApp.torStatusProgress === 100) {
delay(1000, function() { delay(500, function() {
loadingWindow.visible = false loadingWindow.visible = false
mainWindow.visible = true mainWindow.visible = true
}) })