代码拉取完成,页面将自动刷新
import sys
import os
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
class Demo(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
mainLayout = QVBoxLayout()
toolBar = QToolBar()
btn = QPushButton('Add Top')
btn.clicked.connect(self.OnAddTopClicked)
toolBar.addWidget(btn)
btn = QPushButton('Add Child')
btn.clicked.connect(self.OnAddChildClicked)
toolBar.addWidget(btn)
btn = QPushButton('Delete')
btn.clicked.connect(self.OnDelClicked)
toolBar.addWidget(btn)
toolBar.addWidget(QLabel('Update:'))
self.currentEdit = QLineEdit()
toolBar.addWidget(self.currentEdit)
btn = QPushButton('Update')
btn.clicked.connect(self.OnUpdateClicked)
toolBar.addWidget(btn)
self.treeView = QTreeView()
self.treeData = QStandardItemModel()
self.treeView.setModel(self.treeData)
self.treeData.setHorizontalHeaderLabels(['ID','Value'])
mainLayout.addWidget(toolBar)
mainLayout.addWidget(self.treeView)
self.setLayout(mainLayout)
def OnAddTopClicked(self):
id = random.randint(0, 999)
txt = f'Node{id}'
self.treeData.appendRow(QStandardItem(txt))
self.parent.OnShowMessage(f"Add top item '{txt}'")
def OnAddChildClicked(self):
id = random.randint(0, 999)
arr = [f'{id}',f'Node{id}']
currentIndex = self.treeView.currentIndex()
currentItem = self.treeData.itemFromIndex(currentIndex)
childCount = currentItem.rowCount()
#currentItem.setChild(childCount,0,QStandardItem(arr[0]))
#currentItem.setChild(childCount,1,QStandardItem(arr[1]))
currentItem.appendRow([QStandardItem(arr[0]),QStandardItem(arr[1])])
self.treeView.expand(currentIndex)#展开
self.parent.OnShowMessage(f"Add child item '{arr[1]}'")
def OnDelClicked(self):
currentIndex = self.treeView.currentIndex()
#print(f'row:{current.row()},column:{current.column()}')
currentItem =self.treeData.itemFromIndex(currentIndex)
txt =currentItem.text()
if currentItem.parent() is None:
self.treeData.removeRow(currentIndex.row())
else:
currentItem.parent().removeRow(currentIndex.row())
self.parent.OnShowMessage(f"Delete item '{txt}'")
def OnUpdateClicked(self):
currentIndex = self.treeView.currentIndex()
currentItem = self.treeData.itemFromIndex(currentIndex)
old = currentItem.text()
new = self.currentEdit.text()
currentItem.setText(new)
self.parent.OnShowMessage(f"Update item from '{old}' to '{new}'")
def runDemo(parent):
wigdet = Demo(parent)
return wigdet
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。