나는 그것이이 구현 어떻게 볼 수 VCL에 뛰어 들었다. 나는 그렇게하는 것이 좋은 생각인지 주저했다. 왜냐하면이 프레임 워크의 개념은 Qt와는 완전히 다르고 도움이되기보다는 나를 잘못된 길로 인도 할 수 있기 때문이다. 그러나이 질문에 대한 답변이 없으면 Qt 조치가이 방법으로 작동하지 않을 수도 있음을 나에게 알립니다. 어느 쪽이 유감이다.
델파이의 VCL 액션은 이벤트 루프에서 애플리케이션의 유휴 시간으로 평가되는 중앙 목록에 자신을 등록합니다. 등록되지 않은 때 다시
class HFClient(QMainWindow):
def updateAction(self, action):
action.setEnabled(False)
class ActionUpdater(QTimer):
def __init__(self):
super(ActionUpdater, self).__init__()
self.members = {}
self.timeout.connect(self.timerEvent)
def add(self, action, updateProc):
self.members[action] = updateProc
def remove(self, action):
del self.members[action]
def timerEvent(self, unused):
# prevent being the last one to keep the object alive:
done = [action for action in self.members if sys.getrefcount(action) < 5]
for action in done:
self.remove(action)
# call registered method:
for action, updateProc in self.members.items():
updateProc(action)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = HFClient()
w.show()
updater = ActionUpdater()
updater.start()
a = QAction("save as", app)
updater.add(a, w.updateAction)
# del a
app.exec()
메모리 언어는 객체를 가지고 열심히 비트입니다 프로그램 관리 : 내가 타이머를 사용하여 Qt를 에뮬레이트 할 수 있습니다 발견 적어도이 따라서 나는이 주위에 출근 시간 제한은 0으로 시작 등록 된 개체가 완료됩니다. 너무 쉽게 개체는 여전히 레지스터에 참조를 가지고 있기 때문에 살아있게됩니다. 나는 refcount와 updater 인스턴스 내의 참조 수를 비교하여이를 막으려 고 시도했다. 나는 이것을 위해 더 나은 방법을 중요하게 생각한다.
자신을 등록 할 QAction 자손을 만들 수 있습니다 (업데이트를위한 신호 슬롯 조합 일 수도 있음). 물론
class ActionUpdater(QTimer):
def __init__(self):
super(ActionUpdater, self).__init__()
self.members = []
self.timeout.connect(self.timerEvent)
def add(self, action):
self.members.append(weakref.ref(action, self.remove))
def remove(self, action):
self.members.remove(action)
def timerEvent(self, unused):
# call registered method:
for action in self.members:
action().getUpdated()
:
업데이트
종래 신호 슬롯 메커니즘을 사용하려면, 위 코드에서 참조 카운팅 문제를 해결
Weakrefs처럼 행동 자손 http://toc.qt.io/qt-5/qobject.html#sender에 대한 Qt에서 사용 가능하며'qobject_cast'를 사용하여'sender()'에서'QAction' 타입을 얻을 수 있습니다. 'onContentChanging'이 슬롯 함수 여야합니다. 나는 이것에 대한 답을 줄 것이지만 나는 Qt/C++ 만한다. – AlexanderVX
@AlexanderVX OP는 첫 번째 코드 조각과 같은 것을 원하지만 ** 두 번째 코드 조각은 아닙니다. –
미안합니다. SaveActionUpdate 슬롯이 필요하고 거기에 보낸 사람을 처리 할 수 있습니다. – AlexanderVX