来自另一类的PyQt访问项

时间:2018-06-25 09:36:26

标签: python pyqt

我正在使用PyQt,我需要从其他类方法访问列表项,该怎么做?

我想在item_click(self,item)方法中使用self.list_item,并在另一个类方法中使用它。

Class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.LoadList()
        self.show()

    def LoadList(self):
        self.ui = Ui_List()
        self.ui.setupUi(self)

        self.ui.listWidget.itemClicked.connect(self.item_click)
        self.ui.pushButton_Edit.clicked.connect(self.edit_project_btn)

    def item_click(self, item):
        self.list_item = str(item.text())
        self.ui.pushButton_Edit.setEnabled(True)

    def edit_project_btn(self):
        self.dialog = ProjectEdit()
        self.dialog.show()

class ProjectEdit(QtWidgets.QDialog):
    def __init__(self):
        super(ProjectEdit, self).__init__()
        self.ui = Ui_Edit()
        self.ui.setupUi(self)

        self.ui.pushButton_ok.clicked.connect(self.close)
        ### access self.list_item here ####

1 个答案:

答案 0 :(得分:1)

首先,您应该在let obj={ n:1, j:'test' }; let foo = 'j' obj[foo] = 'new val' ; let { j } = obj; alert(j);//'test' 方法中声明list_itemdialog

__init__

然后您可以将参数传递给类构造函数

class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Window, self).__init__()
        self.list_item = None
        self.dialog = None
        self.LoadList()
        self.show()