I'm trying to achieve same thing as mentioned in below post with QML.
Breadcrumbs navigation using QToolBar and QListView
I'm not able to figure out, How to append ToolButton via PySide2 to QML ToolBar and update Gridview relatively (based on given hierarchical data).
main.qml
import QtQuick 2.13
import QtQuick.Controls 2.13
import QtQuick.Layouts 1.13
import QtQuick.Controls.Styles 1.4
ApplicationWindow {
id: mainWindowId
visible: true
width: 960
height: 540
title: qsTr("Breadcrumbs Test")
Rectangle {
width: parent.width
height: parent.height
ColumnLayout {
width: parent.width
height: parent.height
spacing: 6
TextField {
id: filterTextFieldId
Layout.fillWidth: true
Layout.preferredHeight: 40
font {
family: "SF Pro Display"
pixelSize: 22
}
placeholderText: "Type Filter Expression"
color: "dodgerblue"
}
ToolBar {
background: Rectangle {
color: "transparent"
}
RowLayout {
anchors.fill: parent
spacing: 10
ToolButton {
Layout.preferredHeight: 20
contentItem: Text {
text: qsTr('Home')
color: "#FFFFFF"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
radius: 12
color: "#40e0d0"
}
onClicked: crumbsNavigation.on_buttonTriggered()
}
ToolButton {
Layout.preferredHeight: 20
contentItem: Text {
text: qsTr('About')
color: "#FFFFFF"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
radius: 12
color: "#40e0d0"
}
onClicked: crumbsNavigation.on_buttonTriggered()
}
ToolButton {
Layout.preferredHeight: 20
contentItem: Text {
text: qsTr('Contact')
color: "#FFFFFF"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
radius: 12
color: "#40e0d0"
}
onClicked: crumbsNavigation.on_buttonTriggered()
}
}
}
Rectangle {
Layout.fillWidth: true
Layout.fillHeight: true
color: "dodgerblue"
GridView {
id: crumbsViewId
width: parent.width
height: parent.height
anchors.fill: parent
anchors.margins: 12
cellWidth: 130
cellHeight: 130
model: crumbsNavigation.model
delegate: Text {text:qsTr('Hello'); color:"white"}
focus: true
}
}
}
}
}
qmlBreadcrumbs.py
from PySide2 import QtCore, QtQuick, QtGui, QtWidgets, QtQml
import os
import sys
import re
crumbs_data = {"books":{
"web":{
"front-end":{
"html":["the missing manual", "core html5 canvas"],
"css":["css pocket reference", "css in depth"],
"js":["you don't know js", "eloquent javascript"]
},
"back-end":{
"php":["modern php", "php web services"],
"python":["dive into python", "python for everybody",
"Think Python", "Effective Python", "Fluent Python"]
}
},
"database":{
"sql":{
"mysql":["mysql in a nutshell", "mysql cookbook"],
"postgresql":["postgresql up and running", "practical postgresql"]
},
"nosql":{
"mongodb":["mongodb in action", "scaling mongodb"],
"cassandra":["practical cassandra", "mastering cassandra"]
}}}}
def dict_to_model(item, d):
if isinstance(d, dict):
for k, v in d.items():
it = QtGui.QStandardItem(k)
item.appendRow(it)
dict_to_model(it, v)
elif isinstance(d, list):
for v in d:
dict_to_model(item, v)
else:
item.appendRow(QtGui.QStandardItem(str(d)))
class crumbsNavigation(QtCore.QObject):
clicked = QtCore.Signal(QtCore.QModelIndex)
def __init__(self, json_data, parent=None):
super(crumbsNavigation, self).__init__(parent)
self.model = QtGui.QStandardItemModel(self)
dict_to_model(self.model.invisibleRootItem(), json_data)
it = self.model.item(0, 0)
ix = self.model.indexFromItem(it)
@QtCore.Slot(QtCore.QModelIndex)
def on_clicked(self, index):
if not self.model.hasChildren(index):
self.clicked.emit(index)
return
action = self.toolbar.addAction(index.data())
action.setData(QtCore.QPersistentModelIndex(index))
self.listview.setRootIndex(index)
@QtCore.Slot(QtWidgets.QAction)
def on_actionTriggered(self, action):
ix = action.data()
model = ix.model()
self.listview.setRootIndex(QtCore.QModelIndex(ix))
self.toolbar.clear()
ixs = []
while ix.isValid():
ixs.append(ix)
ix = ix.parent()
for ix in reversed(ixs):
action = self.toolbar.addAction(ix.data())
action.setData(ix)
@QtCore.Slot()
def on_buttonTriggered(self):
print('Toolbutton Triggered')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine()
crumbObject = crumbsNavigation(crumbs_data)
engine.rootContext().setContextProperty("crumbsNavigation", crumbObject)
engine.load(QtCore.QUrl.fromLocalFile('E:/Tech/main.qml'))
if not engine.rootObjects():
sys.exit(-1)
engine.quit.connect(app.quit)
sys.exit(app.exec_())