2 Star 10 Fork 7

终點起點/PySide6-UI-Demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
XML.py 3.55 KB
一键复制 编辑 原始数据 按行查看 历史
终點起點 提交于 2024-07-16 17:04 +08:00 . 增加部分控件功能
import sys
import os
import uuid
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from PySide6.QtXml import *
class Demo(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.parent = parent
self.xml=os.path.join(self.parent.getAppDir(),'test.xml')
layout = QVBoxLayout()
layout.setAlignment(Qt.AlignmentFlag.AlignTop)
toolBar=QToolBar()
btn=QPushButton('Create')
btn.clicked.connect(self.Create)
toolBar.addWidget(btn)
btn=QPushButton('Add Root')
btn.clicked.connect(self.AddRoot)
toolBar.addWidget(btn)
btn=QPushButton('Add Child')
btn.clicked.connect(self.AddChild)
toolBar.addWidget(btn)
btn=QPushButton('Add Child with attribute')
btn.clicked.connect(self.AddChildAttribute)
toolBar.addWidget(btn)
btn=QPushButton('Add Child with TextNode')
btn.clicked.connect(self.AddChildTextNode)
toolBar.addWidget(btn)
btn=QPushButton('SaveFile')
btn.clicked.connect(self.SaveFile)
toolBar.addWidget(btn)
btn=QPushButton('LoadFile')
btn.clicked.connect(self.LoadFile)
toolBar.addWidget(btn)
layout.addWidget(toolBar)
self.text=QTextBrowser()#QTextEdit的只读控件
layout.addWidget(self.text)
self.setLayout(layout)
def Create(self):
self.doc= QDomDocument()
self.doc.appendChild(self.doc.createProcessingInstruction('xml',"version=\"1.0\" encoding=\"UTF-8\""))
self.text.setPlainText(self.doc.toString())
def AddRoot(self):
if self.root is None:
self.root=self.doc.createElement('Books')
self.doc.appendChild(self.root)
self.text.setPlainText(self.doc.toString())
def AddChild(self):
child=self.doc.createElement('Book')
self.root.appendChild(child)
self.text.setPlainText(self.doc.toString())
def AddChildAttribute(self):
child=self.doc.createElement('Book')
child.setAttribute('ID',uuid.uuid1().hex)
child.setAttribute('Name',f'BookName_{random.randint(1,100)}')
child.setAttribute('Price',f'{random.randint(1,100)}')
self.root.appendChild(child)
self.text.setPlainText(self.doc.toString())
def AddChildTextNode(self):
child=self.doc.createElement('Book')
child.setAttribute('ID',uuid.uuid1().hex)
child.setAttribute('Name',f'BookName_{random.randint(1,100)}')
child.setAttribute('Price',f'{random.randint(1,100)}')
des=self.doc.createElement('Description')
des.appendChild(self.doc.createTextNode('Book Description'))
child.appendChild(des)
self.root.appendChild(child)
self.text.setPlainText(self.doc.toString())
def SaveFile(self):
file=QFile(self.xml)
file.open(QIODeviceBase.OpenModeFlag.ReadWrite|QIODeviceBase.OpenModeFlag.Truncate)
self.doc.save(QTextStream(file),4)
file.close()
def LoadFile(self):
file=QFile(self.xml)
file.open(QIODeviceBase.OpenModeFlag.ReadOnly)
content=file.readAll().toStdString()
self.text.setPlainText(content)
self.doc= QDomDocument()
#self.doc.setContent(file)#此方式无效,暂时不清楚原因
self.doc.setContent(content)
file.close()
self.root=self.doc.documentElement()
def runDemo(parent):
wigdet = Demo(parent)
return wigdet
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/johnjiangw/pyside6-ui-demo.git
git@gitee.com:johnjiangw/pyside6-ui-demo.git
johnjiangw
pyside6-ui-demo
PySide6-UI-Demo
master

搜索帮助