生成treeWidget树与获取treeWidget树节点的数据
import sys
from PyQt5. QtCore import Qt
from PyQt5. QtWidgets import QApplication, QTreeWidgetItem, QLineEdit, QSpinBox, QComboBox
from PyQt5. QtWidgets import QWidget
from release_test import Ui_FormCASE_CONFIG = { "test1" : [ { "name" : "运行次数" , "value" : QSpinBox, "min" : 0 , "max" : 10 , "default" : 1 } , { "name" : "超时(秒)" , "value" : QSpinBox, "min" : 0 , "max" : 60 , "default" : 0 } , ] , "test2" : [ { "name" : "采集11" , "value" : QLineEdit, "default" : "50020200" } , { "name" : "采集22" , "value" : QLineEdit, "default" : "20210200,20000200,20010200,20010400,20040200,20050200,200A0200,00100200,00000200,00200200" } , { "name" : "采集间隔(分钟)" , "value" : QComboBox, "items" : [ "1" , "5" , "15" ] , "default" : "5" } , { "name" : "超时(秒)" , "value" : QSpinBox, "min" : 0 , "max" : 9999 , "default" : 0 } , ]
} class MainModule ( QWidget, Ui_Form) : def __init__ ( self) : super ( ) . __init__( parent= None ) self. setupUi( self) self. init_case_tree( ) self. save_conf_btn. clicked. connect( self. save_conf) def init_case_tree ( self) : self. treeWidget. setHeaderLabels( [ "参数" , "值" ] ) self. treeWidget. setColumnWidth( 0 , 250 ) for case_name in CASE_CONFIG: case_item = QTreeWidgetItem( [ case_name] ) for conf in CASE_CONFIG[ case_name] : conf_name_item = QTreeWidgetItem( [ conf[ "name" ] ] ) case_item. addChild( conf_name_item) conf_value_item = conf[ "value" ] ( ) if isinstance ( conf_value_item, QSpinBox) : min_value = conf[ "min" ] max_value = conf[ "max" ] default_value = conf[ "default" ] conf_value_item. setRange( min_value, max_value) conf_value_item. setValue( default_value) conf_value_item. setMaximumWidth( 100 ) elif isinstance ( conf_value_item, QComboBox) : items = conf[ "items" ] default_value = conf[ "default" ] conf_value_item. addItems( items) conf_value_item. setCurrentText( default_value) conf_value_item. setMaximumWidth( 100 ) elif isinstance ( conf_value_item, QLineEdit) : default_value = conf[ "default" ] conf_value_item. setText( default_value) self. treeWidget. setItemWidget( conf_name_item, 1 , conf_value_item) case_item. setCheckState( 0 , Qt. Checked) self. treeWidget. addTopLevelItem( case_item) self. treeWidget. expandAll( ) def save_conf ( self) : conf_info = { } case_cnt = self. treeWidget. topLevelItemCount( ) for case_index in range ( case_cnt) : case_item = self. treeWidget. topLevelItem( case_index) case_name = case_item. text( 0 ) conf_info[ case_name] = { } conf_cnt = case_item. childCount( ) print ( case_name) for conf_index in range ( conf_cnt) : conf_item = case_item. child( conf_index) conf_name = conf_item. text( 0 ) conf_value_item = self. treeWidget. itemWidget( conf_item, 1 ) if isinstance ( conf_value_item, QSpinBox) : value = conf_value_item. value( ) elif isinstance ( conf_value_item, QComboBox) : value = conf_value_item. currentText( ) elif isinstance ( conf_value_item, QLineEdit) : value = conf_value_item. text( ) else : value = "" print ( conf_name, value) if __name__ == '__main__' : app = QApplication( sys. argv) mainWindow = MainModule( ) mainWindow. show( ) sys. exit( app. exec ( ) )
test1
运行次数 1
超时( 秒) 0
test2
采集11 50020200
采集22 20210200 , 20000200 , 20010200 , 20010400 , 20040200 , 20050200 , 200A0200, 00100200 , 00000200 , 00200200
采集间隔( 分钟) 5
超时( 秒) 0