我写了一个包含两个独立文件的python GUI程序;一个用于逻辑代码,另一个用于使用PyQt4的GUI。一些对象(按钮,文本字段......)的行为在整个代码中都会发生变化,我需要通过单击QAction类菜单项将所有对象重置为其原始状态。我怎么能这样做?
编辑:应该将GUI重置为原始状态的函数:
def newSession(self):
self.ui.setupUi(self)
self.filename = ""
self.paramsSplitted = []
self.timestep = None
self.index = None
self.selectedParam = None
self.selectedMethod = None
--Snip--
答案 0 :(得分:0)
你能做什么:
ResetHandler(QtCore.QObject)
信号reset_everything
个对象
QApplication
qapplication.reset_handler = ResetHandler()
on_reset_everything_triggered()
个插槽。 (可选:您也可以只使用update
)。reset_everything
上处理程序的全局可用QApplication
信号。QAction.triggered
与ResetHandler.reset_everything
信号相关联。QAction
时,都会调用reset_everything
信号,并且您连接的所有UI元素都会自行更新。答案 1 :(得分:0)
就像您在评论中所要求的那样,这是一种利用函数连接所有信号和方法setupUi
的示意图。
class MainWindow(QtGui.QMainWindow) :
def __init__(self) :
QtGui.QMainWindow.__init__(self)
self.ui.setupUi(self)
# Some code
self.connectAllSignals()
def connectAllSignals(self) :
self.someWidget.clicked.connect(self.someFunction)
self.someAction.triggered.connect(self.otherFunction)
# All the other signals
def disconnectAllSignals(self) :
try :
self.someWidget.clicked.disconnect()
self.someAction.triggered.disconnect()
# All the other signals
except :
print("Something went wrong. Check your code.")
pass
def newSession(self) :
self.ui.setupUi(self)
self.disconnectAllSignals()
self.connectAllSignals()
# Do whatever it takes
通过这种方式,您可以确保只有信号的初始设置,并且所有动态添加的连接都被破坏。在方法disconnectAllSignals
中,确保所有小部件都存在,并且所有信号在您调用它时至少有一个连接。如果您有动态调用的新窗口小部件,则应在调用newSession
后在方法connectAllSignals
中删除它们。