我正在尝试打开一个QDialog
from a QMainWindow
,并且在关闭`QDialog之后,如果我需要再次打开它,它必须打开并显示与我关闭它时相同的信息。
这是代码QMainWindow
:
class A (QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
#I create a QPushButton to open the QDialog
self.axes1 = self.figure_canvas.figure.add_axes ([0.8, 0.01, 0.19, 0.05])
self.button = QPushButton(self.axes1,"Open Dialog")
self.button.on_clicked(self.OpenDialog)
#This is the method to open the QDialog which is in another module
def OpenDialog(self, event):
text = configurePort.ConfigurePort.retrieve_data(self)
print text
这段代码的作用是在我的QMainWindow
当我点击它时,它会打开一个QDialog
,它是在另一个模块中创建的。这是代码QDialog
:
class ConfigurePort(QDialog):
def __init__(self, parent = None):
QDialog.__init__(self, parent)
uic.loadUi("configurePort.ui", self)
#I create a button to check active ports and show them
self.connect(self.btn_checkconn, SIGNAL("clicked()"), self.check_ports)
#This method calls another class which opens another QDialog
#and I select the port that I want
def check_ports(self):
self.check_serial = CheckPorts(self)
self.check_serial.exec_()
#After selecting the port, when I close the QDialog of the class named above
#the port´s name appears in the first QDialog
@classmethod
def retrieve_data(cls, parent = None):
dlg = cls(parent)
dlg.exec_()
text = dlg.getPortText()
return text
def closeEvent(self, event):
#Here is where I need to write the code to close the QDialog
#and it does not has to be an event
在该方法中,closeEvent
,我需要编写必要的代码,以便我可以关闭窗口,并使用打开它时使用的相同按钮,使用关闭窗口时显示的最后信息再次打开它。
我尝试过使用QSettings
但它不起作用(也许我用错了)。我尝试了show()
and hide()
类的PyQt
也是,但是没有用。希望你能帮我。
- - - 编辑 - - -
我编辑了上面的代码。我添加了一些方法以便更好地理解。所以,我打开QDialog
called ConfigurePort
它显示了这一点:
红色圆圈包围着港口的名称。它显示在一个QLabel
,我从QDialog
然后当我关闭时打印它QDialog
。我之所以能做到这一点,要归功于我之前提出的一个问题,该问题位于此链接中:
使用 PyQt 从子进程获取数据 https://stackoverflow.com/questions/37302213/getting-data-from-child-using-pyqt/37307475#37307475
The check_port
上面代码中显示的方法,打开另一个QDialog
效果很好。这样我就可以在我的电脑中选择我需要的端口。所以,这并不重要。
所以,关闭之后QDialog
(并选择例如“COM3”,如图所示),我需要再次打开它,并查看关闭它之前显示的相同信息。
我尝试添加这一行,使用QSettings
:
self.settings = QSettings("MyCompany", "MyApp")
if not self.settings.value("windowsState") == None:
self.restoreState(self.settings.value("windowState"))
但正如我之前所说,我认为我没有正确使用它,但我希望我可以使用更简单的方法来解决这个问题。
----- 编辑 2 -----
感谢@Brendan Abel 的帮助,我有这个代码:
class ConfigurePort(QDialog):
def __init__(self, parent):
super(ConfigurePort, self).__init__(parent)
uic.loadUi("configurePort.ui", self)
self.myValue = 10
self.restoreSettings()
self.connect(self.btn_checkconn, SIGNAL("clicked()"), self.check_ports)
self.buttonBox.button(QDialogButtonBox.Cancel).clicked.connect(self.close)
self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.closeEvent)
self.iniUi()
def check_ports(self):
pass
def iniUi(self):
pass #I just create some QLabels in here
@classmethod
def retrieve_data(cls, parent = None):
dlg = cls(parent)
dlg.exec_()
text = dlg.getPortText()
return text
def closeEvent(self, event):
self.saveSettings()
super(QDialog,self).closeEvent(event)
def saveSettings(self):
settings = QSettings("MyOrg", "MyApp")
settings.setValue("myValue", self.myValue)
def restoreSettings(self):
settings = QSettings("MyOrg", "MyApp")
self.myValue = settings.value("myValue", self.myValue)
这给了我这个错误:TypeError: QWidget.closeEvent(QCloseEvent): argument 1 has unexpected type 'bool'
我知道我错过了一些东西,但我看不到它。