示例#1
0
    def QObject对象名称和属性(self):
        obj0 = QObject()
        obj1 = QObject()
        obj2 = QObject()
        obj3 = QObject()
        obj4 = QObject()
        obj5 = QObject()
        obj2.setObjectName("2")
        obj3.setObjectName("3")
        print("obj0", obj0)
        print("obj1", obj1)
        print("obj2", obj2)
        print("obj3", obj3)
        print("obj4", obj4)
        print("obj5", obj5)

        obj1.setParent(obj0)
        obj2.setParent(obj0)
        obj3.setParent(obj1)
        obj4.setParent(obj2)
        obj5.setParent(obj2)

        print(obj1.parent())
        print(obj2.parent())
        print(obj0.children())
        print(obj0.findChildren(QObject, None, Qt.FindChildrenRecursively))
        print(obj0.findChild(QObject, "2", Qt.FindChildrenRecursively))
        #********************内存管理***************************开始
        obj1 = QObject()
        self.obj1 = obj1
        obj2 = QObject()
        obj2.setParent(obj1)

        # 监听obj2对象被释放

        obj2.destroyed.connect(lambda _: print("obj2对象被释放了"))
        del self.obj1
示例#2
0
class CommandStack(collections.abc.Collection):
    def __init__(self):
        self._parent = QObject(None)
        self._index = 0
        self._command_number = 1

    def __len__(self):
        return len(self._parent.children())

    def __iter__(self):
        self._index = 0
        return self

    def __next__(self):
        if len(self) == 0:
            raise StopIteration

        if self._index < len(self):
            self._index += 1
            return self._parent.children()[self._index - 1]

        raise StopIteration

    def __contains__(self, item):
        if isinstance(item, str):
            # testing by string name and not reference
            item_name = item
            find_me = self._parent.findChild(item_name)
            if find_me is not None:
                return True
            return False

        # testing by references
        for itm in self:
            if item is itm:
                return True

        return False

    def __eq__(self, other):
        equals = [
            len(self) == len(other),
        ]
        equals = equals + list(
            map(lambda x, y: x is y, iter(self), iter(other)))

        return all(equals)

    def __del__(self):
        self._parent.deleteLater()

    def push(self, command):
        if not isinstance(command, QObject):
            raise TypeError

        if command.parent() is not None:
            raise AttributeError('Command QObject already has a parent!')

        command.setParent(self._parent)
        command.setObjectName('Command_{}'.format(self._command_number))
        self._command_number += 1

    def pop(self):
        last_index = len(self) - 1
        last_guy = self._parent.children()[last_index]

        self._command_number -= 1
        last_guy.setParent(None)
        last_guy.setObjectName(None)
        return last_guy