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"`
_ string `property:"torStatusSummary"`
//_ QIdentityListModel `property:"identities"`
_ func() `constructor:"init"`
}
@ -49,8 +51,8 @@ func InitCwtch(engine *qml.QQmlApplicationEngine, qCwtchApp *QmlCwtchApp) error
go func() {
for {
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"])
qCwtchApp.SetTorStatusProgress(progress)
@ -68,6 +70,8 @@ func InitCwtch(engine *qml.QQmlApplicationEngine, qCwtchApp *QmlCwtchApp) error
log.Fatalf("Error initializing application: %v", err)
}
<-doneChan
return nil
@ -89,10 +93,19 @@ func main() {
quickcontrols2.QQuickStyle_SetStyle("Material")
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
engine := qml.NewQQmlApplicationEngine(nil)
//engine.QmlRegisterType()
engine.RootContext().SetContextProperty("cwtchApp", qCwtchApp)
engine.RootContext().SetContextProperty("identityList", identityList)
// load the embeeded qml file
// created by either qtrcc or qtdeploy

View File

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

View File

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

View File

@ -1,5 +1,6 @@
import QtQuick 2.9
import QtQuick.Window 2.2
import CustomQmlTypes 1.0
Window {
//visible: false
@ -8,25 +9,36 @@ Window {
height: 800
title: qsTr("Bulletin")
property alias model: profilesColumn.model
ProfilesColumn {
id: profilesColumn
model: profiles
model: identityList
anchors.top: parent.top
anchors.left: parent.left
height: parent.height
width: 100
}
GroupsColumn {
id: groupsColumn
anchors.left: profilesColumn.right
model: groups
anchors.top: parent.top
anchors.left: profilesColumn.right
height: parent.height
width: 250
}
ListModel {
/*ListModel {
id: profiles
ListElement {name: "alice"; onion: "710829b408e8b368273cbc20b07f1c0d"; pcolor: "#ff0000"}
ListElement {name: "bob"; onion: "a219b9740fc76367833cbc20b07d1cee"; pcolor: "#00ff00" }
ListElement {name: "carol"; onion: "930829b408e8b364563cbc20b07a6560"; pcolor: "#0000ff" }
ListElement {name: "+"; onion: "930829b408e8b364563cbc20b07a6560"; pcolor: "#a0a0a0" }
}
}*/
ListModel {
id: groups
@ -39,7 +51,13 @@ Window {
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 CustomQmlTypes 1.0
Rectangle {
color: "#381F47"
height: parent.height
width: 100
anchors.top: parent.top
anchors.left: parent.left
property alias model: list.model
Text { color: "white"; text: "length: " + cwtchApp.identities.length }
ListView {
id: list
@ -29,8 +26,8 @@ Rectangle {
width: 100
Column {
Rectangle { width: 80; height: 80; color: pcolor }
Text { color: "#a0a0a0"; text: name }
Rectangle { width: 80; height: 80; color: Pcolor }
Text { color: "#a0a0a0"; text: Name }
}
}
}

View File

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