检查 QSettings 是否使用默认值

Check if QSettings is using the default value

有什么方法可以检查 QSettings 是否使用默认值?

例如:

def setup_ui(self):
    self.user_input = QtGui.QLineEdit()
    self.user_input.setText("Input something...")
    ...
    ...

    # Check for any stored settings
    self.user_input.setText(self.settings.value("userInput", "Input something..."))


def load_settings(self):
    self.settings = QtCore.QSettings('TEST', 'My_Tool')
    input = self.settings.value("userInput", "Input something...")


def save_settings(self):
    settings = QtCore.QSettings('TEST', 'My_Tool')
    settings.setValue('userInput', self.user_input.currentText())

如果我重新启动我的工具,有些函数将使用 QLineEdit 中的当前文本字符串。到目前为止,为了让我绕过默认值,我将我的函数编码如下:

def test_func(self, input_text):
    if not input_text == 'Input something...':
        # Do something...

你的问题等同于询问密钥是否存在所以你必须使用contains()方法:

if settings.contains("userInput"):
    # there is the key in QSettings

else:
    # there is no key in QSettings