Closed AlphaLiu closed 1 year ago
在本地用PyQt5简单复现了下,确实存在该问题。经查,作者代码不存在问题,是提问者的代码处理上与作者代码存在隐含冲突。
解决方案:
qconfig.set(cfg.checkList, self.checkList)
改为:
import copy
qconfig.set(cfg.checkList, copy.copy(self.checkList))
或者:
qconfig.set(cfg.checkList, [x for x in self.checkList])
原因:很多人写py的时候会忘记py中不同类型赋值给不同变量到底是引用还是赋值,在这里,字符串赋值给不同变量时它是复制一个值,而list则是将地址给其他变量,类似的还有dict等。set方法部分代码:
def set(self, item, value, save=True):
""" set the value of config item
Parameters
----------
item: ConfigItem
config item
value:
the new value of config item
save: bool
whether to save the change to config file
"""
if item.value == value:
return
如果是list的话,则在最后两行判断地址未发生变化,直接返回,不会保存。
@zhiyiYo 可以关了
Describe the bug 有一个config的值是list,在使用qconfig.set改变时,只有程序启动后的第一次qconfiig.set有实时写入config.json,之后同样的qconfig.set都没有写入。如果是一个值为str的config,则在每次textChanged时调用qconfig.set,都会实时写入config.json。不清楚这是否是bug。我是参考了example 的setting里面的musicFolderList的config用法。
Environment 环境信息
To Reproduce
Code 最小复现代码
Expected behavior qconfig.set能实时把list写入config.json