mouseClickEvent位于ViewBoxCustom.py
中,单击场景时正确触发。compute_spc_map
在SPCanalyse.py
中,ViewBoxCustom.py
不导入它,因为这样会进行循环导入。我知道,但我想避免。你知道吗
来自ViewBoxCustom
的片段
from SPCanalyse import MainWindow #This import should not exist
def mouseClickEvent(self, ev):
elif ev.button() == QtCore.Qt.LeftButton:
ev.accept()
# The following line does not work because MainWindow is **NOT** imported
MainWindow.compute_spc_map(ev.pos().x(), ev.pos().y())
来自SPCanalyse
的片段。SPCanalyse
导入ViewBoxCustom
以便访问函数并生成应用程序功能
from ViewBoxCustom import MultiRoiViewBox # MultiRoiViewBox contains mouseClickEvent
def compute_spc_map(self, x, y):
if not self.preprocessed_frames == None:
self.image = fj.get_correlation_map(y, x, self.preprocessed_frames)
我不能把compute_spc_map
放在ViewBoxCustom
中,因为preprocessed_frames
是在SPCanalyse
中生成和使用的变量
我想把ViewBoxCustom
中的mouseClickEvent
和SPCanalyse
中的compute_spc_map
连接起来,在SPCanalyse
中执行以下操作可能会有用
from ViewBoxCustom import MultiRoiViewBox
self.vb = MultiRoiViewBox(lockAspect=True,enableMenu=True)
self.vb.mouseClickEvent.connect(self.compute_spc_map)
遗憾的是mouseClickEvent
没有属性“connect”
看起来您正试图从子小部件(即
ViewBoxCustom
是MainWindow
的子部件)调用父小部件上的方法。您通常不想这样做,因为它限制了子窗口小部件的灵活性,并且可能导致循环依赖,就像您在这里遇到的那样,当子窗口小部件没有理由依赖于父窗口小部件时。你知道吗这里使用的一个好的设计模式是,子部件发出一个
Signal
,父部件连接到该Signal
,并使用它触发函数的调用(与直接调用函数的子部件相反)。你知道吗就你而言:
ViewBoxCustom
没有理由导入或了解MainWindow
的任何信息。你知道吗相关问题 更多 >
编程相关推荐