1
+
2
+ #!/usr/bin/env python
3
+ # -*- coding: utf-8 -*-
4
+ '''
5
+ @resource:none
6
+ @description: 1. exec()执行动态生成控件 //关闭程序时把model类型保存到ini文件中,打开时生成model对象.
7
+ @Created on 2018年3月17日
8
+ @email: 625781186@qq.com
9
+ '''
10
+
11
+ from PyQt5 import QtGui , QtWidgets , QtCore
12
+ from PyQt5 .QtCore import *
13
+ from PyQt5 .QtGui import *
14
+ from PyQt5 .QtWidgets import *
15
+ from PyQt5 .QtCore import pyqtSlot
16
+ from PyQt5 .QtWidgets import QDialog
17
+ from PyQt5 .QtSql import *
18
+ import re
19
+
20
+
21
+ from Ui_getModel import Ui_Dialog
22
+
23
+
24
+ class Dialog (QDialog , Ui_Dialog ):
25
+ """
26
+ Class documentation goes here.
27
+ """
28
+ def __init__ (self , parent = None ):
29
+ super (Dialog , self ).__init__ (parent )
30
+ self .setupUi (self )
31
+ self .setting = QSettings ('./setting.ini' , QSettings .IniFormat )
32
+ self .getModel ()
33
+ self .tableView .setModel (self .qmodel )
34
+
35
+ print ('1:' , self .tableView .model ())
36
+ def closeEvent (self , e ):
37
+ text = re .split ('\.| ' , str (self .tableView .model ()))
38
+ if text != ['None' ]:
39
+ i = [i for i , x in enumerate (text ) if x .find ('Model' )!= - 1 ]
40
+ # print(text[i[-1]])
41
+ self .setting .setValue ("Model/model" ,text [i [- 1 ]]+ '()' );#设置key和value,也就是参数和值
42
+ def getModel (self ):
43
+ if (self .setting .contains ("Model/model" )): #此节点是否存在该数据
44
+ model = self .setting .value ("Model/model" )
45
+ exec ('''self.qmodel=%s''' % (model ))#python exec()执行字符串命令
46
+ print ('2:' , self .qmodel )
47
+
48
+ if __name__ == "__main__" :
49
+ import sys
50
+ app = QtWidgets .QApplication (sys .argv )
51
+
52
+ ui = Dialog ()
53
+
54
+ ui .show ()
55
+ sys .exit (app .exec_ ())
0 commit comments