保存QComboBox选择。Q设置?

2024-06-07 07:06:48 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图保存QComboBox的选择设置(就像你看到的复选框一样),这样当用户关闭应用程序并重新打开时,它会加载以前选择的设置。你知道吗

如您所见,我尝试使用QSettings for QComboBox,但每次关闭并重新打开GUI时,ComboBox都无法正确保存选定的设置。。它跳到索引1。你知道吗

Image of the UI/GUI

   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))

Tags: selfboxsettingssavedefcheckconnectstate