因此,我试图保存QComboBox的选择设置(就像您所看到的CheckBox一样),以便当用户关闭应用程序并重新打开时,它会加载以前选择的设置。
如您所见,我尝试对QComboBox使用QSettings,但是每次关闭并重新打开GUI时,ComboBox都无法正确保存所选设置。它跳到索引1。
self.comboBox = QComboBox()
grid_layout.addWidget(self.comboBox, 4, 0)
grid_layout.addItem(QSpacerItem(0, 0, QSizePolicy.Expanding, QSizePolicy.Expanding), 4, 1)
self.comboBox.addItem("Choose a Number")
self.comboBox.addItem("1")
self.comboBox.addItem("2")
self.comboBox.addItem("3")
self.comboBox.addItem("4")
# self.comboBox.highligh
self.comboBox.activated.connect(self.randomFunction)
self.check_box.clicked.connect(self.checkBox1Print)
self.check_box2.clicked.connect(self.checkBox2Print)
# Get settings
settings = QSettings()
# Get checkbox state
check_state = settings.value(SETTINGS_TRAY, False, type=bool)
# Set state
self.check_box.setChecked(check_state)
self.check_box2.setChecked(check_state)
self.comboBox.setCurrentIndex(check_state)
# connect the slot to the signal by clicking
self.check_box.clicked.connect(self.save_check_box_settings)
self.check_box2.clicked.connect(self.save_checkbox2_settings)
self.comboBox.activated.connect(self.save_ComboBox_settings)
# Slot checkbox to save the settings
def save_check_box_settings(self):
settings = QSettings()
settings.setValue(SETTINGS_TRAY, self.check_box.isChecked())
settings.sync()
def save_checkbox2_settings(self):
settings = QSettings()
settings.setValue(SETTINGS_TRAY, self.check_box2.isChecked())
settings.sync()
def save_ComboBox_settings(self):
settings = QSettings()
settings.setValue(SETTINGS_TRAY, self.comboBox.currentIndex())
settings.sync()
def checkBox1Print (self):
print("Checkbox 1 Clicked")
def checkBox2Print (self):
print("Checkbox 2 Clicked")
def randomFunction(self, index):
print(self.comboBox.itemText(index))
答案 0 :(得分:0)
您要保存与SETTINGS_TRAY键相关的所有信息,而必须创建一个名为SETTINGS_TRAY的组并为每个信息指定一个键:
from PyQt5 import QtCore, QtGui, QtWidgets
SETTINGS_TRAY = "Tray"
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.checkbox_1 = QtWidgets.QCheckBox("Settings Checkbox for minimizing to tray")
self.checkbox_2 = QtWidgets.QCheckBox("This checkbox")
self.combobox = QtWidgets.QComboBox()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QLabel("Application, which can minimize to Tray"))
lay.addWidget(self.checkbox_1)
lay.addWidget(self.checkbox_2)
lay.addWidget(self.combobox)
lay.addStretch()
self.combobox.addItems(["Choose a Number", "1", "2", "3","4"])
self.readSettings()
# connections
self.combobox.currentTextChanged.connect(self.on_currentTextChanged)
self.checkbox_1.stateChanged.connect(self.on_checked_1)
self.checkbox_2.stateChanged.connect(self.on_checked_2)
def readSettings(self):
settings = QtCore.QSettings()
settings.beginGroup(SETTINGS_TRAY)
current_item = settings.value("option", type=str)
if current_item is None:
self.combobox.setCurrentIndex(0)
else:
self.combobox.setCurrentText(current_item)
is_checked_1 = settings.value("checked_1", QtCore.Qt.Unchecked, type=QtCore.Qt.CheckState)
self.checkbox_1.setChecked(is_checked_1)
is_checked_2 = settings.value("checked_2", QtCore.Qt.Unchecked, type=QtCore.Qt.CheckState)
self.checkbox_2.setChecked(is_checked_2)
settings.endGroup()
@QtCore.pyqtSlot(str)
def on_currentTextChanged(self, text):
settings = QtCore.QSettings()
settings.beginGroup(SETTINGS_TRAY)
settings.setValue("option", text)
settings.endGroup()
@QtCore.pyqtSlot(int)
def on_checked_1(self, state):
settings = QtCore.QSettings()
settings.beginGroup(SETTINGS_TRAY)
settings.setValue("checked_1", state)
settings.endGroup()
@QtCore.pyqtSlot(int)
def on_checked_2(self, state):
settings = QtCore.QSettings()
settings.beginGroup(SETTINGS_TRAY)
settings.setValue("checked_2", state)
settings.endGroup()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(640, 240)
w.show()
sys.exit(app.exec_())