打开 QDialog 并保存上次状态 [英] Opening a QDialog and saving last state

查看:87
本文介绍了打开 QDialog 并保存上次状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 QMainWindow 打开一个 QDialog,在关闭 `QDialog 后,如果我需要再次打开它,它必须打开并显示与我关闭时相同的信息.

I am trying to open a QDialog from a QMainWindow, and after closing the `QDialog, if I need to open it again, it has to open and show the same information that had when I close it.

这是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的代码:

What this code does is create a button in my QMainWindow and when I click it, it opens a QDialog, which is created in another module. And this is the code of the 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,我需要编写必要的代码,以便我可以关闭窗口,并使用我用来打开它的相同按钮,使用最后的信息再次打开它当我关闭它时它会显示出来.

In the method, closeEvent, I need to write the necessary code, so I can close the window, and using the same button that I use to open it, open it again with the last information that it showed when I closed it.

我尝试使用 QSettings 但它没有用(也许我用错了).我也尝试了 PyQtshow()hide() 类,但是没有用.希望你能帮助我.

I have tried to use QSettings but it did not worked (maybe I used it wrong). And I tried the show() and hide() classes of PyQt too, but it did not work. Hope you can help me.

我编辑了上面的代码.我添加了一些方法以便更好地理解.因此,我打开名为 ConfigurePortQDialog 并显示:

I edited the code of above. and I added some methods for a better understanding. So, i open the QDialog called ConfigurePort and it shows this:

红色圆圈环绕着港口的名称.它显示在 QLabel 中,我从 QDialog 中获取此文本,然后在关闭 QDialog 时打印它.由于我之前问过的一个问题,我完成了这个,这在这个链接中:

The red circle, surrounds the port´s name. It is shown in a QLabel,and I take this text from the QDialog and then print it when I close the QDialog. I acomplish this thanks to a question I asked before, wich is in this link:

使用 PyQt 从孩子那里获取数据

上面代码中显示的 check_port 方法会打开另一个效果很好的 QDialog.有了这个,我可以在我的电脑中选择我需要的端口.所以,这并不重要.

The check_port method shown in the code above, opens another QDialog that works great. With this I can select the ports that I need in my pc. So, this does not matter.

因此,在关闭 QDialog(并选择例如COM3",如图所示)后,我需要再次打开它,并看到之前显示的相同信息我关了.

So, after closing the QDialog(and selecting for example "COM3", as you can see in the picture), I need to open it again, and see the same information that was shown before I closed it.

我尝试使用 QSettings 添加此行:

I tried to add this lines, using QSettings :

self.settings = QSettings("MyCompany", "MyApp")
  if not self.settings.value("windowsState") == None:
    self.restoreState(self.settings.value("windowState"))

但正如我之前所说,我认为我没有正确使用它,但我希望我使用更简单的东西来解决这个问题.

But as I said before, I think that I did not use it right, but I hope that I solve this using something simpler.

感谢@Brendan Abel 的帮助,我有这个代码:

Thank to the help of @Brendan Abel I have this code:

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'我知道我错过了一些东西,但我看不到它.

This gives me this error: TypeError: QWidget.closeEvent(QCloseEvent): argument 1 has unexpected type 'bool' I know that I am missing something, but I can not see it.

推荐答案

有几种方法可以持久化这些数据 一般来说,要跨会话持久化数据,你可以使用 QSettings 并将数据加载到__init__ 并将其保存在 closeEvent 方法

There are a couple ways you could persist this data Generally, to persist data across sessions, you use QSettings and load the data in the __init__ and save it in the closeEvent method

通常看起来像这样.这也假设您使用 QVariant api 的 v2 版本;否则,从 QSettings.value 返回的结果将是 QVariant 并且您需要将其转换为适当的 Python 类型.如果您使用的是最新版本的 PyQt,那么您应该使用 v2但如果没有,你可以强制它把它贴在你的文件顶部

Generally it looks something like this. This also assumes your using the v2 version of the QVariant api; otherwise, the results returned from QSettings.value is going to be a QVariant and you'll need to cast it to the appropriate python type. If you're using a recent version of PyQt then you should be on v2, but if not you can force it by sticking this at the top of your file

import sip
sip.setapi('QVariant', 2)
sip.setapi('QString', 2)


class MyDialog(QDialog):

    def __init__(self, parent):
        super(MyDialog, self).__init__(parent)
        self.myvalue = 10
        self.restoreSettings()

    def closeEvent(self, event):
        self.saveSettings()
        super(MyDialog, 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)

您的代码中的错误是由以下原因引起的:

The error in your code is caused by this:

self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.closeEvent)

您不应直接调用或连接到 closeEvent.相反,您应该连接到 .close.accept

You shouldn't be calling or connecting to closeEvent directly. Instead, you should connect to .close or .accept

self.buttonBox.button(QDialogButtonBox.Ok).clicked.connect(self.accept)

这篇关于打开 QDialog 并保存上次状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆