拥有QCheckBox
需要在关闭文件后保存并恢复checkstate
。
试着跟随,但并没有真正的工作!
代码:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class SecondOrder(QtWidgets.QWidget):
def __init__(self, parent=None):
super(SecondOrder, self).__init__(parent)
self.setFont(QtGui.QFont("Helvetica", 8, QtGui.QFont.Normal, italic=False))
curvaturelabel = QtWidgets.QLabel('Nominal curvature')
stiffnesslabel = QtWidgets.QLabel('Nominal stiffness')
simcurvaturelabel = QtWidgets.QLabel('Simplified nominal curvature')
self.curvaturecheck = QtWidgets.QCheckBox()
self.stiffnesscheck = QtWidgets.QCheckBox()
self.simcurvaturecheck = QtWidgets.QCheckBox()
self.clay = QtWidgets.QVBoxLayout()
self.clay.addWidget(curvaturelabel)
self.clay.addWidget(stiffnesslabel)
self.clay.addWidget(simcurvaturelabel)
self.dlay = QtWidgets.QVBoxLayout()
self.dlay.addWidget(self.curvaturecheck)
self.dlay.addWidget(self.stiffnesscheck)
self.dlay.addWidget(self.simcurvaturecheck)
grid = QtWidgets.QGridLayout(self)
grid.addLayout(self.dlay, 0, 0)
grid.addLayout(self.clay, 0, 1)
Secondorder_Group = QtWidgets.QGroupBox(self)
Secondorder_Group.setTitle("&Second order analysis method")
Secondorder_Group.setLayout(grid)
self.elay = QtWidgets.QHBoxLayout(self)
self.elay.addWidget(Secondorder_Group)
self.elay.setSizeConstraint(self.elay.SetFixedSize)
group = QtWidgets.QButtonGroup(self)
group.addButton(self.curvaturecheck)
group.addButton(self.stiffnesscheck)
group.addButton(self.simcurvaturecheck)
self.readSettings()
self.curvaturecheck.stateChanged.connect(lambda: self.writeSettings())
self.stiffnesscheck.stateChanged.connect(lambda: self.writeSettings())
self.simcurvaturecheck.stateChanged.connect(lambda: self.writeSettings())
def readSettings(self):
settings = QtCore.QSettings('file.ini')
settings.beginGroup("QCheckBox")
self.curvaturecheck.setCheckState(settings.value("A", QtCore.Qt.Checked, bool))
self.stiffnesscheck.setCheckState(settings.value("B", False, bool))
self.simcurvaturecheck.setCheckState(settings.value("C", False, bool))
settings.endGroup()
def writeSettings(self):
settings = QtCore.QSettings('file.ini')
settings.beginGroup("QCheckBox")
settings.setValue("A", self.curvaturecheck.isCheckable())
settings.setValue("B", self.stiffnesscheck.isCheckable())
settings.setValue("C", self.simcurvaturecheck.isCheckable())
settings.endGroup()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = SecondOrder()
w.show()
sys.exit(app.exec_())
请注意,尝试了以下代码,它确实可以保存和恢复,但它混合了多个检查?希望能够检查一次并恢复。
settings.setValue("A", self.curvaturecheck.isChecked())
settings.setValue("B", self.stiffnesscheck.isChecked())
settings.setValue("C", self.simcurvaturecheck.isChecked())
如果要保存类型T的数据,则必须使用使用类型T的函数,在这种情况下,您使用isChecked((保存布尔值,并且您希望使用使用Qt::CheckState类型的函数setCheckState((来导致错误。
考虑到以上情况,有两种解决方案:
- 使用布尔:
def readSettings(self):
settings = QtCore.QSettings("file.ini")
settings.beginGroup("QCheckBox")
self.curvaturecheck.setChecked(settings.value("A", True, bool))
self.stiffnesscheck.setChecked(settings.value("B", False, bool))
self.simcurvaturecheck.setChecked(settings.value("C", False, bool))
settings.endGroup()
def writeSettings(self):
settings = QtCore.QSettings("file.ini")
settings.beginGroup("QCheckBox")
settings.setValue("A", self.curvaturecheck.isChecked())
settings.setValue("B", self.stiffnesscheck.isChecked())
settings.setValue("C", self.simcurvaturecheck.isChecked())
settings.endGroup()
- 使用Qt::CheckState
def readSettings(self):
settings = QtCore.QSettings("file.ini")
settings.beginGroup("QCheckBox")
self.curvaturecheck.setCheckState(
settings.value("A", QtCore.Qt.Checked, QtCore.Qt.CheckState)
)
self.stiffnesscheck.setCheckState(
settings.value("B", QtCore.Qt.Unchecked, QtCore.Qt.CheckState)
)
self.simcurvaturecheck.setCheckState(
settings.value("C", QtCore.Qt.Unchecked, QtCore.Qt.CheckState)
)
settings.endGroup()
def writeSettings(self):
settings = QtCore.QSettings("file.ini")
settings.beginGroup("QCheckBox")
settings.setValue("A", self.curvaturecheck.checkState())
settings.setValue("B", self.stiffnesscheck.checkState())
settings.setValue("C", self.simcurvaturecheck.checkState())
settings.endGroup()
建议您删除位于settings.fileName()
中的.ini文件。
另一方面,不需要使用lambda函数,将代码更改为:
self.curvaturecheck.stateChanged.connect(self.writeSettings)
self.stiffnesscheck.stateChanged.connect(self.writeSettings)
self.simcurvaturecheck.stateChanged.connect(self.writeSettings)
https://doc.qt.io/qt-5/qbuttongroup.html#details
QButtonGroup提供了一个抽象容器,按钮小部件可以放在其中。。它不提供该容器的可视化表示(有关容器小部件,请参阅QGroupBox(,而是管理组中每个按钮的状态。
每个复选框上的setValue()
可以切换另一个复选框。
但真正的问题是isCheckable()
(意味着可以检查或不检查(。您可能想要使用isChecked()
(值设置为true(。
编辑。您可以尝试通过setChecked()
设置状态。
或者,如果您想支持部分检查,请将setCheckState()
(如您所用(与checkState()
一起使用以获取信息。