1. <small id='NoECZ'></small><noframes id='NoECZ'>

    2. <legend id='NoECZ'><style id='NoECZ'><dir id='NoECZ'><q id='NoECZ'></q></dir></style></legend>
      • <bdo id='NoECZ'></bdo><ul id='NoECZ'></ul>
    3. <i id='NoECZ'><tr id='NoECZ'><dt id='NoECZ'><q id='NoECZ'><span id='NoECZ'><b id='NoECZ'><form id='NoECZ'><ins id='NoECZ'></ins><ul id='NoECZ'></ul><sub id='NoECZ'></sub></form><legend id='NoECZ'></legend><bdo id='NoECZ'><pre id='NoECZ'><center id='NoECZ'></center></pre></bdo></b><th id='NoECZ'></th></span></q></dt></tr></i><div id='NoECZ'><tfoot id='NoECZ'></tfoot><dl id='NoECZ'><fieldset id='NoECZ'></fieldset></dl></div>
      <tfoot id='NoECZ'></tfoot>

        即使小部件关闭,如何在 PyQt 中的 QLineEdits 中保

        时间:2023-08-07
            <tbody id='3kMhP'></tbody>

              <bdo id='3kMhP'></bdo><ul id='3kMhP'></ul>
              <tfoot id='3kMhP'></tfoot><legend id='3kMhP'><style id='3kMhP'><dir id='3kMhP'><q id='3kMhP'></q></dir></style></legend>

                  <small id='3kMhP'></small><noframes id='3kMhP'>

                • <i id='3kMhP'><tr id='3kMhP'><dt id='3kMhP'><q id='3kMhP'><span id='3kMhP'><b id='3kMhP'><form id='3kMhP'><ins id='3kMhP'></ins><ul id='3kMhP'></ul><sub id='3kMhP'></sub></form><legend id='3kMhP'></legend><bdo id='3kMhP'><pre id='3kMhP'><center id='3kMhP'></center></pre></bdo></b><th id='3kMhP'></th></span></q></dt></tr></i><div id='3kMhP'><tfoot id='3kMhP'></tfoot><dl id='3kMhP'><fieldset id='3kMhP'></fieldset></dl></div>
                • 本文介绍了即使小部件关闭,如何在 PyQt 中的 QLineEdits 中保存文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  您好,我已经使用 PyQt 为我的脚本制作了一个 GUI,我有几个行编辑和几个按钮

                  Hello I have made a GUI for my script using PyQt I have a couple of Line edit ands a couple of buttons

                  (.....) = (self.(.....).text()) 我将该文本用于我的脚本作为变量(但我认为这对问题并不重要)我希望能够在 QLineEdits 中输入文本并保存它,所以下次我打开它时,文本仍然存在

                  (.....) = (self.(.....).text()) which I use that text for my script as a variable (but I don't think thats important to the question) I want to be able to type text into the QLineEdits and for it to save so next time I open it the text will still be there

                  我使用 PyQt5 然后我使用 Py-installer 将它变成一个应用程序所以我希望能够将文本保存在 QLineEdits 中,然后当它关闭时保存在那里以备下次打开它>

                  I use PyQt5 then I use Py-installer to make it into an app So I want to be able to save the text inside the QLineEdits and then when It closes for it be be saved there for next time I open it>

                  附言.我正在与其他人共享这个应用程序所以我希望它保存用户输入的内容(他们正在输入他们自定义的东西,例如(名称或类似的东西)

                  Ps. I am sharing this app with other people So I want it to save what that user puts in (they are putting in stuff that is custom to them like for example (name or something like that)

                  这是我的 pyqt5 代码示例:

                  Here is a sample of my pyqt5 code:

                  推荐答案

                  对于较老的应用程序,它实现了保存和恢复小部件状态的功能.

                  For an older application, it implements the functions that saved the states of the widgets and restored them.

                  为了使其正常工作,应用程序必须满足以下要求:

                  In order for it to work properly, the application must meet the following requirements:

                  • 您必须设置 OrganizationNameOrganizationDomainApplicationName.

                  您要保存状态的每个小部件都必须有一个 objectName

                  Each widget that you want to save the state must have an objectName

                  当你想恢复状态时,你必须使用 restore(),一个不错的选择是在创建所有的小部件之后.

                  You must use restore() when you want to restore the states, a good option is after creating all the widgets.

                  当你想保存状态时,你必须使用save(),一个好地方是closeEvent().

                  You must use save() when you want to save the states, a good place would be closeEvent().

                  在下一部分中,我将展示一个示例:

                  In the next part I show an example:

                  import sys
                  
                  from PyQt5 import QtWidgets, QtCore
                  
                  # for PyQt4 change QtWidget to QtGui and PyQt5 to PyQt4
                  
                  
                  def restore(settings):
                      finfo = QtCore.QFileInfo(settings.fileName())
                      if finfo.exists() and finfo.isFile():
                          for w in QtWidgets.qApp.allWidgets():
                              mo = w.metaObject()
                              if w.objectName() and not w.objectName().startswith("qt_"):
                                  settings.beginGroup(w.objectName())
                                  for i in range( mo.propertyCount(), mo.propertyOffset()-1, -1):
                                      prop = mo.property(i)
                                      if prop.isWritable():
                                          name = prop.name()
                                          val = settings.value(name, w.property(name))
                                          if str(val).isdigit():
                                              val = int(val)
                                          w.setProperty(name, val)
                                  settings.endGroup()
                  
                  def save(settings):
                      for w in QtWidgets.qApp.allWidgets():
                          mo = w.metaObject()
                          if w.objectName() and not w.objectName().startswith("qt_"):
                              settings.beginGroup(w.objectName())
                              for i in range(mo.propertyCount()):
                                  prop = mo.property(i)
                                  name = prop.name()
                                  if prop.isWritable():
                                      settings.setValue(name, w.property(name))
                              settings.endGroup()
                  
                  
                  class Widget(QtWidgets.QWidget):
                      def __init__(self, parent=None):
                          super().__init__(parent)
                          self.setObjectName("widget")
                          self.init_ui()
                          self.settings = QtCore.QSettings()
                          print(self.settings.fileName())
                          restore(self.settings)
                  
                      def init_ui(self):
                          lay = QtWidgets.QVBoxLayout(self)
                          lineEdit1 = QtWidgets.QLabel("label")
                          lineEdit1.setObjectName("label")
                          lineEdit2 = QtWidgets.QLineEdit()
                          lineEdit2.setObjectName("lineEdit2")
                          combobox = QtWidgets.QComboBox()
                          combobox.addItems(["1", "2", "3"])
                          combobox.setObjectName("combo")
                          lay.addWidget(lineEdit1)
                          lay.addWidget(lineEdit2)
                          lay.addWidget(combobox)
                  
                      def closeEvent(self, event):
                          save(self.settings)
                          super().closeEvent(event)
                  
                  
                  if __name__ == '__main__':
                      app = QtWidgets.QApplication(sys.argv)
                      QtCore.QCoreApplication.setOrganizationName("Eyllanesc")
                      QtCore.QCoreApplication.setOrganizationDomain("eyllanesc.com")
                      QtCore.QCoreApplication.setApplicationName("MyApp")
                      ex = Widget()
                      ex.show()
                      sys.exit(app.exec_())
                  

                  <小时>

                  更新:

                  如果您使用 Qt Designer,则不再需要放置 objectsName,因为它们已经建立,但另一方面,提供 Qt Designer 的类不是小部件,而是一个负责填充小部件的类,因此我们必须创建小部件才能覆盖 closeEvent 方法,如下所示:

                  In the case that you use Qt Designer it is no longer necessary to place the objectsNames because they are already established, but on the other hand the class that provides Qt Designer is not a widget, but a class that is responsible for filling a widget, so we must create the widget to be able to overwrite the closeEvent method as shown below:

                  from PyQt5 import QtCore, QtGui, QtWidgets
                  
                  class Ui_MainWindow(object):
                      def setupUi(self, MainWindow):
                          ...
                      def retranslateUi(self, MainWindow):
                          ...
                  
                  class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
                      def __init__(self, *args, **kwargs):
                          super().__init__(*args, **kwargs)
                          self.setupUi(self)
                          self.settings = QtCore.QSettings()
                          restore(self.settings)
                  
                      def closeEvent(self, event):
                          save(self.settings)
                          super().closeEvent(event)
                  
                  if __name__ == "__main__":
                      import sys
                      app = QtWidgets.QApplication(sys.argv)
                      QtCore.QCoreApplication.setOrganizationName("Eyllanesc")
                      QtCore.QCoreApplication.setOrganizationDomain("eyllanesc.com")
                      QtCore.QCoreApplication.setApplicationName("MyApp")
                      w = MainWindow()
                      w.show()
                      sys.exit(app.exec_())
                  

                  这篇关于即使小部件关闭,如何在 PyQt 中的 QLineEdits 中保存文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 PyQt5 运行命令并获取标准输出和标准错误 下一篇:在画布上擦笔

                  相关文章

                  最新文章

                    <bdo id='UbQoR'></bdo><ul id='UbQoR'></ul>
                • <small id='UbQoR'></small><noframes id='UbQoR'>

                  <tfoot id='UbQoR'></tfoot>

                  1. <legend id='UbQoR'><style id='UbQoR'><dir id='UbQoR'><q id='UbQoR'></q></dir></style></legend>
                    1. <i id='UbQoR'><tr id='UbQoR'><dt id='UbQoR'><q id='UbQoR'><span id='UbQoR'><b id='UbQoR'><form id='UbQoR'><ins id='UbQoR'></ins><ul id='UbQoR'></ul><sub id='UbQoR'></sub></form><legend id='UbQoR'></legend><bdo id='UbQoR'><pre id='UbQoR'><center id='UbQoR'></center></pre></bdo></b><th id='UbQoR'></th></span></q></dt></tr></i><div id='UbQoR'><tfoot id='UbQoR'></tfoot><dl id='UbQoR'><fieldset id='UbQoR'></fieldset></dl></div>