4
votes

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.

3

3 Answers

5
votes

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_())
5
votes

Simply make the lineEdit uneditable by making it False:

self.lineEdit.setEnabled(False)
0
votes

If you want the text to be selectable then use:

self.lineEdit.readOnly