zoukankan      html  css  js  c++  java
  • Pyqt 获取动态生成的QLineEdit值

    动态生成控件参考上一篇: http://www.cnblogs.com/dcb3688/p/4588814.html

    那如何获取动态生成控件的值呢?

    比如,动态的生成10个输入框QLineEdit,输入值后,要获取每一个LineEdit的值。 在这里我们通过字典dict{} 来存储控件,循环dict 获取控件的text()

    #!/usr/bin/python  
    # -*-coding:utf-8-*-
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    
    # 主入口文件
    class MainWidget(QDialog):
        def __init__(self, parent=None):
            super(MainWidget, self).__init__(parent)
            self.setMinimumSize(100, 100)
            self.setWindowFlags(Qt.CustomizeWindowHint|Qt.WindowCloseButtonHint)
            self.setWindowOpacity(0.9)
    
            # 添加头部group
            self.headDict = {}
            self.headPostArrayKey = 0
            self.HeadGroupBox = QGroupBox(u'动态添加控件数据')
            self.HeadGroupBox.setMinimumHeight(100)  #高度最小值
            self.HeadGroupBox.scroll(100,2)
            self.HeadAddParam = QPushButton(u'+')
            self.headDict[str(self.headPostArrayKey)+'_key'] = QLineEdit()
            self.headDict[str(self.headPostArrayKey)+'_value'] = QLineEdit()
            self.HeadGroupBoxLayout = QGridLayout()
            self.HeadGroupBoxLayout.addWidget(self.HeadAddParam, 0, 0)
            self.HeadGroupBoxLayout.addWidget(QLabel(u'Key:'), 1, 0)
            self.HeadGroupBoxLayout.addWidget(self.headDict[str(self.headPostArrayKey)+'_key'], 1, 1)
            self.HeadGroupBoxLayout.addWidget(QLabel(u'Value:'), 1, 2)
            self.HeadGroupBoxLayout.addWidget(self.headDict[str(self.headPostArrayKey)+'_value'], 1, 3)
            self.HeadGroupBox.setLayout(self.HeadGroupBoxLayout)
            self.HeadAddParam.clicked.connect(self.addHeadParam)
    
    
            # 提交按钮
            self.btnPost = QPushButton(u'提交')
            self.postbtnLoayout = QHBoxLayout()
            self.postbtnLoayout.addStretch()
            self.postbtnLoayout.addWidget(self.btnPost)
            # Main布局
            main_layout = QVBoxLayout()
            main_layout.addWidget(self.HeadGroupBox)
            main_layout.addLayout(self.postbtnLoayout)  # addLayout 添加的是 Layout
            main_layout.setSpacing(0)
            self.setLayout(main_layout)
    
            self.connect(self.btnPost, SIGNAL('clicked()'), self.postData)
    
    
        def postData(self):
            self.headdictdata={}
            for k, v in self.headDict.items():
                temp=k.split('_')
                if temp[1]=='key':
                    if self.headdictdata.has_key(temp[0]):
                        self.headdictdata[temp[0]]['key'] =str(v.text())
                    else:
                        self.headdictdata[temp[0]] = {'key':str(v.text())}
    
                elif temp[1]=='value':
                    if self.headdictdata.has_key(temp[0]):
                        self.headdictdata[temp[0]]['value'] =str(v.text())
                    else:
                        self.headdictdata[temp[0]] = {'value':str(v.text())}
    
            print(self.headdictdata)
    
        # 添加头部Data
        def addHeadParam(self):
            sts=str(self.headPostArrayKey+1)
            self.headDict[sts+'_key'] = QLineEdit(sts+'name')
            self.headDict[sts+'_value'] = QLineEdit(sts+'chrome')
    
            self.HeadGroupBoxLayout.addWidget(QLabel(u'Key'))
            self.HeadGroupBoxLayout.addWidget(self.headDict[sts+'_key'])
            self.HeadGroupBoxLayout.addWidget(QLabel(u'Value'))
            self.HeadGroupBoxLayout.addWidget(self.headDict[sts+'_value'])
            self.headPostArrayKey+=1
    
    
    
    if __name__ == "__main__":
        import sys
        app = QApplication(sys.argv) 
        main_widget = MainWidget()
        main_widget.show()
        sys.exit(app.exec_())

    效果:

    打印数据:

    {'1': {'value': '1chrome', 'key': '1name'}, '0': {'key': 'keyname', 'value': 'htmls'}, '3': {'value': '3chrome', 'key': '3name'}, '2': {'value': '2chrome', 'key': '2name'}}
  • 相关阅读:
    kNN算法python实现和简单数字识别的方法
    python3.4学习笔记(二十) python strip()函数 去空格 函数的用法
    SolrCloud分布式集群部署步骤
    hbases索引技术:Lily HBase Indexer介绍
    HBase1.0以上版本的API改变
    Hbase1.0 客户端api
    java.io.IOException: Could not locate executable nullinwinutils.exe in the Hadoop binaries
    Resharp最新破解方法
    Cloudera Manager 5和CDH5离线安装
    Apache Thrift
  • 原文地址:https://www.cnblogs.com/dcb3688/p/4607945.html
Copyright © 2011-2022 走看看