Python 如何从treeWidgetItem中获取comboBox.currentText()和SpinBox.text()
上下文: 我有一个QTreeWidgetItem,我想将其保存为JSON文件。 QTreeWidgetItem由三列组成[“名称”、“类型”、“值”]。名称是行编辑,类型是组合框,值是旋转框。我想检索每个字段和每行的值,将它们保存在JSON中 尝试: 检索名称没有问题,但是我找不到如何从comboBox和spinBox中检索值 我首先尝试以最简单的形式检索数据(节点是QTreeWidgetItem):Python 如何从treeWidgetItem中获取comboBox.currentText()和SpinBox.text(),python,python-3.x,pyqt,pyqt5,qtreewidgetitem,Python,Python 3.x,Pyqt,Pyqt5,Qtreewidgetitem,上下文: 我有一个QTreeWidgetItem,我想将其保存为JSON文件。 QTreeWidgetItem由三列组成[“名称”、“类型”、“值”]。名称是行编辑,类型是组合框,值是旋转框。我想检索每个字段和每行的值,将它们保存在JSON中 尝试: 检索名称没有问题,但是我找不到如何从comboBox和spinBox中检索值 我首先尝试以最简单的形式检索数据(节点是QTreeWidgetItem): type=node.text(1) 值=节点。文本(2) 我尝试了QTreeWidgetIt
type=node.text(1)
值=节点。文本(2)
我尝试了QTreeWidgetItem.data(int列,int角色),但不太理解此方法所需的参数“role”
一些代码:
python文件:
从qgis.PyQt导入uic、QtCore、QtGui
导入操作系统,系统
导入qgis.core
导入日志记录
尝试:
从qgis.PyQt.QtGui导入(QWidget、QDialog、QMainWindow、QApplication、QTreeWidgetItem、,
QMenu、QToolButton、QSpinBox、QDoubleSpinBox、QHeaderView、QLineEdit、,
QTextBrowser、QTreeWidgetItemIterator、QComboBox、QLabel、UserRole)
除恐怖外:
从qgis.PyQt.qtwidts导入(QWidget、QDialog、QMainWindow、QApplication、QTreeWidgetItem、,
QMenu、QToolButton、QSpinBox、QDoubleSpinBox、QHeaderView、QLineEdit、,
QTextBrowser、QTreeWidgetItemIterator、QComboBox、QLabel)
导入pprint
导入json
AMCWindow类(QDialog):
定义初始化(self,parent=None,dbase=None):
super(AMCWindow,self)。\uuuuuu init\uuuuuuuuuu(父项=父项)
uipath=os.path.join(os.path.dirname(_文件__),'amcwindow1.ui')
uic.loadUi(uipath,self)
self.dict={}
#树梢
self.treeWidget.setColumnCount(3)
self.treeWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.treeWidget.customContextMenuRequested.connect(self.openMenu)
self.maintreewdgitem=QTreeWidgetItem(self.treeWidget.invisibleRootItem(),['note'])
headerlist=[“名称”、“类型”、“值”]
self.treeWidget.setHeaderItem(QTreeWidgetItem(headerlist))
#右键单击条件
self.menu=QMenu()
self.menu.addAction(self.tr(“添加标准”))
self.menu.addAction(self.tr(“删除条件”))
self.menu.triggered.connect(self.menuAction)
#点击保存
self.buttonSave.clicked.connect(self.saveClicked)
self.menuitem=None
self.itemnamediting=None
def openMenu(自身,位置):
self.menuitem=self.treeWidget.currentItem()
self.menu.exec_(self.treeWidget.viewport().mapToGlobal(位置))
def菜单操作(自身、操作名称):
如果actionname.text()==self.tr(“添加条件”):
qtreewidgetitm=QTreeWidgetItem(self.menuitem,[“新标准”])
#类型
qComboBox=qComboBox()
qComboBox.addItems([“Pondération”,“Classe”])
self.treeWidget.setItemWidget(qtreewidgetitm,1,qComboBox)
#setProperty(“widgetitem”,qtreewidgetitm)
#价值观
spinbox=QDoubleSpinBox()
self.treeWidget.setItemWidget(qtreewidgetitm,2,spinbox)
self.menuitem.setExpanded(True)
返回qtreewigetitm
elif actionname.text()==self.tr(“删除条件”):
self.menuitem.parent().removeChild(self.menuitem)
def saveClicked(自):
打印(“>单击保存”)
自我命令清除
self.visitTree(self.maintreewdgitem、self.dict)
#定义目录
directory=“C://Users//jean.robertou//Desktop//file.json”
#创建文件并将queryDict写入json
打开(目录“w”)作为文件:
json.dump(self.dict,文件)
def visitTree(自身、节点、dct):
"""
在树上迭代并构建dct
:param node:QTreeWidgetItem,当前节点
:param dct:dict,包含来自当前节点的元素
:返回:
"""
打印(“输入visitTree”)
打印(“>visitTree:”,node.text(0))
#获取子对象的数量
childNr=node.childCount()
#如果节点是叶
如果childNr==0:
打印(“>”,node.text(0),“没有子项”)
type=node.text(1)#我对您的代码有以下评论:
- 我认为您在以下行中出现了错误:
self.treeWidget.setItemWidget(qtreewidgetitm,2,qComboBox)
和self.treeWidget.setItemWidget(qtreewidgetitm,3,spinbox)
,因为列是2和3,但Qt中的索引从0开始,所以它们必须分别是1和2
- 添加子项时,请使用与上一个不同的文本,因为在构建字典时,将出现问题,因为不会显示重复的元素。这对于项目的实现可能不是必需的,但是在测试当前代码时,请注意这个问题
- 如果要获取小部件中显示的数据,必须首先使用以下方法获取小部件
考虑到上述情况,解决方案是:
AMCWindow类(QDialog):
定义初始化(self,parent=None,dbase=None):
super(AMCWindow,self)。\uuuuuu init\uuuuuuuuuu(父项=父项)
uipath=os.path.join(os.path.dirname(_文件__),“amcwindow1.ui”)
uic.loadUi(uipath,self)
self.dict={}
#树梢
self.treeWidget.setColumnCount(3)
self.treeWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.treeWidget.customContextMenuRequested.connect(self.openMenu)
self.maintreewdgitem=QTreeWidgetItem(
self.treeWidget.invisibleRootItem(),[“注意”]
)
headerlist=[“名称”、“类型”、“值”]
赛尔夫树
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>941</width>
<height>518</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,10,1,1">
<item>
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Nom</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QToolButton" name="buttonSave">
<property name="text">
<string>Save</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTreeWidget" name="treeWidget">
<property name="dragEnabled">
<bool>true</bool>
</property>
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="animated">
<bool>true</bool>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
<property name="columnCount">
<number>3</number>
</property>
<attribute name="headerDefaultSectionSize">
<number>50</number>
</attribute>
<attribute name="headerStretchLastSection">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
<column>
<property name="text">
<string notr="true">2</string>
</property>
</column>
<column>
<property name="text">
<string notr="true">3</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QFrame" name="frame_2">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QLineEdit" name="lineEdit_sqlfinal"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Final SQL</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QToolButton" name="toolButton_testsql">
<property name="text">
<string>Test SQL</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QTextBrowser" name="textBrowser_res">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>90</height>
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>