【问题标题】:Configure Widgets in a Sub-Dialog in a pyqt Main Window在 pyqt 主窗口的子对话框中配置小部件
【发布时间】:2014-01-11 18:17:58
【问题描述】:

我有一个带有主对话框的 GUI 应用程序,我向它添加了一个按钮。 按下按钮会添加另一个“对话框”,用户必须在其中输入一些值。 两个 Ui 文件都是用 QTDesigner 编写的,“对话框”有一个对象名为“tableCo”的“QtableWidget”我不知道为什么我不能更改这个 tableWidget 的属性:

from PyQt4 import QtGui, QtCore, Qt  
from Main_Window import Ui_Dialog as Dlg
from dialog import Ui_MyDialog

class MainDialog(QtGui.QDialog, Dlg): 
    def __init__(self): 
        QtGui.QDialog.__init__(self) 
        self.setupUi(self)

        self.connect(self.buttonOK, 
                QtCore.SIGNAL("clicked()"), self.onOK) 
        self.connect(self.buttonAbbrechen, 
                QtCore.SIGNAL("clicked()"), self.onClose)

        self.connect(self.Button, 
                QtCore.SIGNAL("clicked()"), self.on_Button_clicked)

    def on_Button_clicked(self, checked=None):
        if checked==None: return
        dialog = QtGui.QDialog()
        dialog.ui = Ui_MyDialog()
        dialog.ui.setupUi(dialog)
        dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        dialog.exec_()


        some_list=["A","B","C"] 
        #a list in another python class from another script that changes so
        #the table properties have to be changed dynamically
        #here I just take a simple list as an example

        #the following two lines do not work (they work if tableCo is an 
        #object in the Main Dialog

        self.tableCo.setColumnCount(len(some_list))           
        self.tableCo.setHorizontalHeaderLabels(some_list)

    def onOK:
    ...
    def onClose:
    ...

如果我按下按钮,我会看到我的“tableCo”小部件,但标题的属性没有改变,关闭此子对话框后,我收到以下错误消息

Traceback (most recent call last):
  File "C:/gui.py", line 88, in on_Button_clicked
    self.tableCo.setColumnCount(len(some_list))
AttributeError: 'MainDialog' object has no attribute 'tableCo'

要在子对话框中配置小部件,我必须在代码中进行哪些更改?

【问题讨论】:

  • 您可以将包含tableCo 的代码粘贴到此处或codepad.org 或其他地方吗?

标签: python pyqt qt-designer


【解决方案1】:

您的on_Button_clicked 中的代码有两个问题。

首先,您尝试在对话框关闭之后调用方法。当调用exec_ 时,对话框进入阻塞循环,直到用户关闭对话框。当对话框关闭时,将执行以下行,但在此之后,当函数返回时,对话框将立即被垃圾回收。

其次,您尝试使用self 访问对话框的方法,而不是通过本地名称dialog,这就是您获得AttributeError 的原因。

您可以通过为第二个对话框创建子类来解决这些问题,方法与为 MainDialog 类创建的方法相同:

class SubDialog(QtGui.QDialog, Ui_MyDialog): 
    def __init__(self, some_list, parent=None): 
        QtGui.QDialog.__init__(self, parent) 
        self.setupUi(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.tableCo.setColumnCount(len(some_list))           
        self.tableCo.setHorizontalHeaderLabels(some_list)

class MainDialog(QtGui.QDialog, Dlg):  
    ...    

    def on_Button_clicked(self, checked=None):
        if checked is None: return
        dialog = SubQDialog(some_list)
        dialog.exec_()

【讨论】:

  • @user2956831。在用户进行更改并dialog.exec_() 返回后,您仍然可以访问其方法、属性等。因此您可以使用例如dialog.tableCo.item(0, 0).text() 获取文本值,然后通过self 将其传递给主对话框。
【解决方案2】:

你确定tableCo 有那个确切的名称并且它直接作为主窗口的父级吗?似乎属性没有被更新只是因为没有self.tableCo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多