2013-04-27 22 views
0

试图了解如何将字典传递给QDialog函数并找回编辑过的字典,我搜索了但找不到任何可以遵循的东西。 我正在使用模态调用,因此我需要在继续之前关闭对话框。通过QDialog编辑字典不返回正确的数据

到目前为止的问题是,应用程序获取尽可能远的returnAttributes()函数,此时它会打印出意外的字符串并且不会关闭。

dialog_grid.py

#!/usr/bin/env python 

import sys 
from PyQt4 import QtGui, QtCore 

class grid_dialog(QtGui.QWidget): 

    def __init__(self, **orig_attr): 
     super(grid_dialog, self).__init__() 
     self.initUI(**orig_attr) 

    def initUI(self, **orig_attr): 
     self.new_attr = orig_attr.copy() 

     nameLabel = QtGui.QLabel("Name:") 
     self.new_attr["name"] = QtGui.QLineEdit(orig_attr["name"]) 
     nameBox = QtGui.QHBoxLayout() 
     nameBox.addStretch(1) 
     nameBox.addWidget(nameLabel) 
     nameBox.addWidget(self.new_attr["name"]) 

     shapeLabel = QtGui.QLabel("Shape:") 
     self.new_attr["shape"] = QtGui.QLineEdit(orig_attr["shape"]) 
     shapeBox = QtGui.QHBoxLayout() 
     shapeBox.addStretch(1) 
     shapeBox.addWidget(shapeLabel) 
     shapeBox.addWidget(self.new_attr["shape"]) 

     unitLabel = QtGui.QLabel("Unit:") 
     self.new_attr["unit"] = QtGui.QLineEdit(orig_attr["unit"]) 
     unitBox = QtGui.QHBoxLayout() 
     unitBox.addStretch(1) 
     unitBox.addWidget(unitLabel) 
     unitBox.addWidget(self.new_attr["unit"]) 

     scaleLabel = QtGui.QLabel("Scale:") 
     self.new_attr["scale"] = QtGui.QLineEdit(orig_attr["scale"]) 
     scaleBox = QtGui.QHBoxLayout() 
     scaleBox.addStretch(1) 
     scaleBox.addWidget(scaleLabel) 
     scaleBox.addWidget(self.new_attr["scale"]) 

     depthLabel = QtGui.QLabel("Depth:") 
     self.new_attr["depth"] = QtGui.QLineEdit(orig_attr["depth"]) 
     depthBox = QtGui.QHBoxLayout() 
     depthBox.addStretch(1) 
     depthBox.addWidget(depthLabel) 
     depthBox.addWidget(self.new_attr["depth"]) 

     planeLabel = QtGui.QLabel("Plane:") 
     self.new_attr["plane"] = QtGui.QLineEdit(orig_attr["plane"]) 
     planeBox = QtGui.QHBoxLayout() 
     planeBox.addStretch(1) 
     planeBox.addWidget(planeLabel) 
     planeBox.addWidget(self.new_attr["plane"]) 

     originLabel = QtGui.QLabel("Origin:") 
     self.new_attr["origin"] = QtGui.QLineEdit(orig_attr["origin"]) 
     originBox = QtGui.QHBoxLayout() 
     originBox.addStretch(1) 
     originBox.addWidget(originLabel) 
     originBox.addWidget(self.new_attr["origin"]) 

     acceptButton = QtGui.QPushButton('Accept') 
     acceptButton.clicked.connect(self.returnAttributes) 

     cancelButton = QtGui.QPushButton('Cancel') 
     cancelButton.clicked.connect(self.discardAttributes) 

     actionBox = QtGui.QHBoxLayout() 
     actionBox.addStretch(1) 
     actionBox.addWidget(acceptButton) 
     actionBox.addWidget(cancelButton) 

     attribBox = QtGui.QVBoxLayout() 
     attribBox.addStretch(1) 
     attribBox.addLayout(nameBox) 
     attribBox.addLayout(shapeBox) 
     attribBox.addLayout(unitBox) 
     attribBox.addLayout(scaleBox) 
     attribBox.addLayout(depthBox) 
     attribBox.addLayout(planeBox) 
     attribBox.addLayout(originBox) 
     attribBox.addLayout(actionBox) 

     self.setLayout(attribBox)  
     self.setGeometry(300, 300, 250, 150) 
     self.setWindowTitle('Attributes') 
     self.show() 

    # this is not working 
    def returnAttributes(self): 
     print self.new_attr 
     return self.new_attr 
     QtGui.QDialog.close(self) 

    def discardAttributes(self): 
     QtGui.QDialog.close(self) 




def main(): 
    # Test case 
    app = QtGui.QApplication(sys.argv) 

    part_attr = {} 
    part_attr["name"] = "name1" 
    part_attr["shape"] = "shape1" 
    part_attr["unit"] = "unit1" 
    part_attr["scale"] = "scale1" 
    part_attr["depth"] = "depth1" 
    part_attr["plane"] = "plane1" 
    part_attr["origin"] = "origin1" 
    part_attr["action"] = "action1" 

    # Print original data  
    print part_attr 

    # Edit data 
    part_attr = grid_dialog(**part_attr) 

    # Print edited data 
    print part_attr 

    sys.exit(app.exec_()) 

if __name__ == "__main__": 
    main() 

从素文字输出:

[email protected]:~/Python_scripts/dialog$ ./dialog_grid.py 
{'origin': 'origin1', 'scale': 'scale1', 'name': 'name1', 'shape': 'shape1', 'depth': 'depth1', 'plane': 'plane1', 'action': 'action1', 'unit': 'unit1'} 
<__main__.grid_dialog object at 0xb70a64ac> 
{'origin': <PyQt4.QtGui.QLineEdit object at 0xb70a6a4c>, 'scale': <PyQt4.QtGui.QLineEdit object at 0xb70a67c4>, 'name': <PyQt4.QtGui.QLineEdit object at 0xb70a653c>, 'depth': <PyQt4.QtGui.QLineEdit object at 0xb70a689c>, 'shape': <PyQt4.QtGui.QLineEdit object at 0xb70a6614>, 'plane': <PyQt4.QtGui.QLineEdit object at 0xb70a6974>, 'action': 'action1', 'unit': <PyQt4.QtGui.QLineEdit object at 0xb70a66ec>} 
[email protected]:~/Python_scripts/dialog$ 

回答

0
  1. 类应该有驼峰的名字:代替GridDialoggrid_dialog

  2. 您呼叫类grid_dialog,但从QtGui.QWidget继承它?通常用于显示带有字段的表格以设置一些数据QDialog被使用。

  3. 这是错误的:

    def discardAttributes(self): 
        QtGui.QDialog.close(self) 
    

    首先,因为2点二的,你可以用self.close()

  4. 你可以使你的代码更易读,如果使用这样的事情:

    def __init__(self, **orig_attr): 
        super(grid_dialog, self).__init__() 
        self.initUI(**orig_attr) 
    
    def initUI(self, name, shape, unit, ...): 
        ... 
    
  5. 要传递一些数据到对话框并从中获得一些结果,通常使用QDialog

一个对话框窗口是主要用于短期任务 并与用户简要通讯的顶级窗口。

QDialog的一个特殊性是它可以是acceptedrejected。当模式对话框被接受/拒绝时,您将获得相应的返回码,但窗口未关闭,而是隐藏,因此您可以读取对话框中的所有值。

所以,你需要从QDialog继承类和更改

acceptButton.clicked.connect(self.returnAttributes) 
    cancelButton.clicked.connect(self.discardAttributes) 

acceptButton.clicked.connect(self.accept) 
    cancelButton.clicked.connect(self.reject) 

而且改变:

nameLabel = QtGui.QLabel("Name:") 
    self.new_attr["name"] = QtGui.QLineEdit(orig_attr["name"]) 
    nameBox = QtGui.QHBoxLayout() 
    nameBox.addStretch(1) 
    nameBox.addWidget(nameLabel) 
    nameBox.addWidget(self.new_attr["name"]) 

nameLabel = QtGui.QLabel("Name:") 
    self.nameEdit = QtGui.QLineEdit(name) 
    nameBox = QtGui.QHBoxLayout() 
    nameBox.addStretch(1) 
    nameBox.addWidget(nameLabel) 
    nameBox.addWidget(self.nameEdit) 

然后使用类似:

dialog = GridDialog(...) 
return_code = dialog.exec() 
if return_code == QtGui.QDialog.Accepted: 
    # get the data 
    dialog.nameEdit.text() 
+0

感谢您的详细解释,我发现很难拿起Python语言,这有助于。我会做出你所建议的改变。 – salatwork 2013-04-29 23:31:50

+0

感谢您的帮助,我做出了改变,并发挥了作用。是否有可能将字典中的编辑对象传递回调用程序?我想在未来会让这个功能更加灵活,假设我知道如何动态地创建标签和编辑对象。 – salatwork 2013-04-30 00:51:44

+0

>我发现很难拿起Python语言<这不是关于Python,而是关于Qt框架。 >是否有可能将编辑的对象在字典中传回给调用程序<制作封装函数,它将打开传递初始数据的模式窗体,将分析返回代码并将数据提取出来并返回带有数据的字典。同样,这不是关于Python,而是关于所谓的编程模式。 – warvariuc 2013-04-30 07:16:06