def test_tooltree(self): tree = ToolTree() role = tree.actionRole() model = QStandardItemModel() tree.setModel(model) item = QStandardItem("One") item.setData(QAction("One", tree), role) model.appendRow([item]) cat = QStandardItem("A Category") item = QStandardItem("Two") item.setData(QAction("Two", tree), role) cat.appendRow([item]) item = QStandardItem("Three") item.setData(QAction("Three", tree), role) cat.appendRow([item]) model.appendRow([cat]) def p(action): print("triggered", action.text()) tree.triggered.connect(p) tree.show() self.qWait()
def test_tooltree(self): tree = ToolTree() role = tree.actionRole() model = QStandardItemModel() tree.setModel(model) item = QStandardItem("One") item.setData(QAction("One", tree), role) model.appendRow([item]) cat = QStandardItem("A Category") item = QStandardItem("Two") item.setData(QAction("Two", tree), role) cat.appendRow([item]) item = QStandardItem("Three") item.setData(QAction("Three", tree), role) cat.appendRow([item]) model.appendRow([cat]) def p(action): print("triggered", action.text()) tree.triggered.connect(p) tree.show() self.app.exec_()
def _tree_to_model( tree: Dict, root: QStandardItem, sel_model: QItemSelectionModel ) -> None: # tuple of subtree and selection flag if isinstance(tree, tuple): tree, _ = tree # read from .json if isinstance(tree, list): tree = {t: {} for t in tree} for word, words in tree.items(): item = QStandardItem(word) root.appendRow(item) if isinstance(words, tuple): _, selected = words if selected: sel_model.select(item.index(), QItemSelectionModel.Select) if len(words): _tree_to_model(words, item, sel_model)