PySide "propagate" 如何发出信号?

How does PySide "propagate" signals?

这是我所拥有的大大简化的版本。 类 S、L 和 O 是三个不同的 UI,具有一些共同的组件,包括 [prev] 和 [next] 按钮。

当我将所有内容从 class Broken 的 init() 移动到 main() 时,上一个和下一个按钮在单击时连接到它们的插槽。然而,当他们处于破碎状态时,他们不会。 Broken 会,但是会看到上一个按钮的文本并打印它。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from PySide.QtCore import *
from PySide.QtGui  import *

import sys

import S
import L
import O


class MainWindow():
    def __init__(self, Module, initValues):
        self.module = Module(initValues)
        print(self.module.instruction.prev.text())            # Works!   Yay!
        self.module.instruction.prev.clicked.connect(self.p)  # Doesn't! Boo!
        self.module.instruction.next.clicked.connect(self.n)  # Doesn't! Boo!

    def p(self):
        print("Previous pressed")

    def n(self):
        print("Next pressed")


class Broken():
    def __init__(self, why, width, height):
        """When I put the following in main(), everything works"""
        initValues = {}

        if   why == "S":
            ui = MainWindow(S.UI, initValues)
        elif why == "L":
            ui = MainWindow(L.UI, initValues)
        elif why == "O":
            ui = MainWindow(O.UI, initValues)


def main():
    why = input("S, L, O? ")

    app = QApplication(sys.argv)

    screen_resolution = app.desktop().screenGeometry()
    width, height = screen_resolution.width(), screen_resolution.height()

    broken = Broken(why, width, height)  # UI shows, button clicks don't
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

再一次,@ekhumoro "schooled" 我,虽然我以后可能会再次让自己难堪。

通过为所有内容添加父项以防止激进的垃圾收集解决了该问题。