我有一个简单的QMainWindow
,其中包含一个按钮,点击该按钮时会弹出一个子QDialog
。在这个子对话框中,只有一个QLineEdit
。
我想使用QLineEdit
在INI file
上保存此QSettings
的值:
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtCore import QSettings
from PyQt5.QtWidgets import QDialog, QMainWindow
from wintest import Ui_MainWindow
from test import Ui_Test
class TestDialog(QDialog, Ui_Test):
def __init__(self, parent):
QDialog.__init__(self, parent)
self.setupUi(self)
self.pushButton_save.clicked.connect(self.close)
def saveSettings(self):
settings = QSettings("settings.ini", QSettings.IniFormat)
settings.setValue('key/value', self.lineEdit_test.text())
def closeEvent(self, event):
print('closing, saving settings...')
self.saveSettings() # value are not saved
event.accept()
# main UI
class WinMain(QMainWindow, Ui_MainWindow):
settings = QSettings()
def __init__(self, dialog):
QMainWindow.__init__(self)
self.setupUi(dialog)
self.pushButton_openTestDialog.clicked.connect(self.showTestDialog)
def showTestDialog(self):
dialog = TestDialog(self)
dialog.ui = Ui_Test()
dialog.ui.setupUi(dialog)
ok = dialog.exec_()
dialog.show()
# main()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.addLibraryPath('.')
window = QtWidgets.QMainWindow()
pid = WinMain(window)
window.show()
sys.exit(app.exec_())
永远不会保存lineEdit_test
的值...我在INI file
的结果是:
[key]
value=
修改:包含.ui文件
TestDialog
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Test(object):
def setupUi(self, Test):
Test.setObjectName("Test")
Test.resize(349, 160)
self.label = QtWidgets.QLabel(Test)
self.label.setGeometry(QtCore.QRect(30, 50, 71, 16))
self.label.setObjectName("label")
self.pushButton_save = QtWidgets.QPushButton(Test)
self.pushButton_save.setGeometry(QtCore.QRect(140, 110, 75, 23))
self.pushButton_save.setObjectName("pushButton_save")
self.lineEdit_test = QtWidgets.QLineEdit(Test)
self.lineEdit_test.setGeometry(QtCore.QRect(110, 50, 161, 20))
self.lineEdit_test.setObjectName("lineEdit_test")
self.retranslateUi(Test)
QtCore.QMetaObject.connectSlotsByName(Test)
def retranslateUi(self, Test):
_translate = QtCore.QCoreApplication.translate
Test.setWindowTitle(_translate("Test", "Dialog"))
self.label.setText(_translate("Test", "MyLineEdit"))
self.pushButton_save.setText(_translate("Test", "save settings"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Test = QtWidgets.QDialog()
ui = Ui_Test()
ui.setupUi(Test)
Test.show()
sys.exit(app.exec_())
主窗口
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'wintest.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(480, 298)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_openTestDialog = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_openTestDialog.setGeometry(QtCore.QRect(200, 120, 75, 23))
self.pushButton_openTestDialog.setObjectName("pushButton_openTestDialog")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_openTestDialog.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())