我正在编写一个我正在编写的程序。由于它非常大,我想将布局和信号等分成单独的类/模块,以使事情更容易阅读。我希望能够从另一个类编辑布局小部件。这是可能的,还是我想做一些不可能的事情?我在下面列举了一个例子,以防我的解释不清楚
class Layout:
def __init__(self):
self.callback = CallBack()
def Gui(self):
'''
some layout with a listwidget that affects another listwidget depending on choice for example
'''
self.list1 = QtGui.QListWidget()
self.list1.addItems(['chocolate', 'candy', 'pop'])
self.list1.itemClicked.connect(self.callback.ButtonCallback)
self.list2 = QtGui.QListWidget()
class CallBack(Layout):
def __init__(self)
super(CallBack, self).__init__()
def ButtonCallback(self, button_signal):
'''
do get options for self.list2 depending on chosen item
'''
new_items = ['item1', 'item2', 'item3']
Layout.list2.addItems(new_items)
我意识到代码的最后一行可能是错误的,但那是我正在努力的部分。
非常感谢您的帮助。
答案 0 :(得分:0)
只要符合QObject,您当然可以让其他对象为您回答信号。在您编写的示例代码中,我将创建CallBack对象作为QObject的子类,并保留与拥有它的Layout的关系,以便它可以访问Layout对象的属性。
class Layout:
def __init__(self):
self.callback = CallBack(self)
def Gui(self):
'''
some layout with a listwidget that affects another listwidget depending on choice for example
'''
self.list1 = QtGui.QListWidget()
self.list1.addItems(['chocolate', 'candy', 'pop'])
self.list1.itemClicked.connect(self.callback.ButtonCallback)
self.list2 = QtGui.QListWidget()
class CallBack(QtCore.QObject):
def __init__(self, parent)
super(CallBack, self).__init__()
self.parent = parent
def ButtonCallback(self, button_signal):
'''
do get options for self.parent.list2 depending on chosen item
'''
new_items = ['item1', 'item2', 'item3']
self.parent.list2.addItems(new_items)