Issue
I would like to make it so under certain conditions, it is impossible to edit a QLineEdit widget. Ideally, it would look something like:
QLE_On = QCheckBox("Non-editable?")
generic = QLineEdit()
if QLE_On.isChecked():
#disable editing of generic
Looking at the docs, .isReadOnly might be one possible option how to achieve what I'm looking for, but I'm not quite sure how to implement that.
Solution
To be able to establish that the QLineEdit
is editable or you should not use the setReadOnly()
function.
You can know the state of the checkbox synchronously and asynchronously through the checkState()
function and the stateChanged
signal. In your case you need both, the first to set the initial value and the second when you do a check through the GUI, in your case the following code is the solution:
generic.setReadOnly(QLE_On.checkState()!=Qt.Unchecked)
QLE_On.stateChanged.connect(lambda state: generic.setReadOnly(state!=Qt.Unchecked))
Example:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
app = QApplication(sys.argv)
w=QWidget()
w.setLayout(QVBoxLayout())
QLE_On = QCheckBox("Non-editable?")
generic = QLineEdit()
generic.setReadOnly(QLE_On.checkState()!=Qt.Unchecked)
QLE_On.stateChanged.connect(lambda state: generic.setReadOnly(state!=Qt.Unchecked))
w.layout().addWidget(QLE_On)
w.layout().addWidget(generic)
w.show()
sys.exit(app.exec_())
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.