I have a Pyqt Widget containing 3 buttons, 1 QlineEdit and 1 statusbar. One of the buttons makes the qlineedit in Read Only state, another one to disable the qlineedit Readonly state and the last one to show the values of the Qlineedit in the status bar message.
I would like to build an event that is triggered when the qlineedit is in read only state and is clicked, this should show us a message in a status bar that the field is protected.
Lastly, I would like to build another event that changes the color of the button "Edit" When the Each Time the Qlineedit Read only State is disabled.
```
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit
from PyQt5.QtWidgets import QPushButton, QStatusBar
from PyQt5.QtCore import QSize
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(750, 300))
self.setWindowTitle("PyQt Line Edit example (textfield) - pythonprogramminglanguage.com")
self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
# Push buttons
self.btn_add = QPushButton('View', self)
self.btn_add.clicked.connect(self.view)
self.btn_add.resize(200,32)
self.btn_add.move(80, 60)
self.btn_edit = QPushButton('Edit', self)
self.btn_edit.clicked.connect(self.edit)
if self.edit():
self.btn_edit.clicked.connect(self.change_edit_button_color) #<<<<<<<<<<< This is my attempt
## The problem is It doesn´t get disabled when I press other button
else:
self.btn_edit.clicked.connect(self.normal_edit_button_color)
self.btn_edit.resize(200,32)
self.btn_edit.move(80, 120 )
self.btn_Validate = QPushButton('Validate', self)
self.btn_Validate.clicked.connect(self.Validate)
self.btn_Validate.resize(200,32)
self.btn_Validate.move(80, 180)
#Creating the statusbar
self.statusBar = QStatusBar()
self.setStatusBar(self.statusBar)
self.line.setReadOnly(True) #<<<< BY DEFAULT THE QLINE FIELD IS IN READONLY STATE
#>>>>>>>>>>>>>>>If the Qlineedit object is in readonly state notify us>>> This is my attempt <<<<<<<<<<<<<<<<<<<<<<<<<
#The problem is that it doesn´t take into account the if statetment
if self.line.isReadOnly():
self.line.selectionChanged.connect(self.Protected_field)
def view(self):
self.line.setReadOnly(True) #<<<< FUCTION TO PROTECT THE QLINE FIELD BY READONLY STATE
self.edit= False
def edit(self):
self.line.setReadOnly(False) #<<<< FUCTION TO DISABLE READONLY STATE THE QLINE FIELD
return True
def Validate(self):
self.statusBar.showMessage("Qline Value is {}".format(self.line.text()), 8000)
self.edit= False
def Protected_field(self): #<<< show in statusbar a message when the Qline is in Readonly state and is clicked
self.statusBar.showMessage("This field is protected", 2000)
def change_edit_button_color(self): #<<< Function to change edit button color when the QLINE is not in Readonly State
self.btn_edit.setStyleSheet('background-color :rgb(119, 120, 121)')
def normal_edit_button_color(self): #<<< Function to change edit button color when the QLINE is not in Readonly State
self.btn_edit.setStyleSheet('background-color :white')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
´´´
I created the signal "selectionChanged" to notify us each time the Qlineedit object is clicked with an if statement but it doesn´t take that if statement into account, it triggers always the signal.
For the color change, I made the edit method return a boolean True value when it's clicked and the other buttons return False and created an if statement with the signal to change the button color each time the Edit method returns True, but the problem is the button doesn´t comeback to the normal color once the the boolean value is False.
CodePudding user response:
There is no need to create additional slots for the same signal, you can simply change the color of the button inside the methods that toggle the readOnly
setting. In fact you actually don't even need two separate buttons for the view
and edit
methods... you could just have a single button that toggles readOnly
both off and on.
Also your if statement
for the statusbar message isn't working because the code needs to be executed every time the button is pressed, so it needs to moved inside of the slot method.
For example:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(750, 300))
self.setWindowTitle("PyQt Line Edit example (textfield) - pythonprogramminglanguage.com")
self.nameLabel = QLabel(self)
self.nameLabel.setText('Name:')
self.line = QLineEdit(self)
self.line.move(80, 20)
self.line.resize(200, 32)
self.nameLabel.move(20, 20)
self.btn_add = QPushButton('View', self)
self.btn_add.clicked.connect(self.view)
self.btn_add.resize(200,32)
self.btn_add.move(80, 60)
self.btn_edit = QPushButton('Edit', self)
self.btn_edit.clicked.connect(self.edit)
self.btn_edit.resize(200,32)
self.btn_edit.move(80, 120 )
self.btn_Validate = QPushButton('Validate', self)
self.btn_Validate.clicked.connect(self.Validate)
self.btn_Validate.resize(200,32)
self.btn_Validate.move(80, 180)
self.statusBar = QStatusBar()
self.setStatusBar(self.statusBar)
self.line.setReadOnly(True)
self.line.selectionChanged.connect(self.Protected_field)
def view(self):
self.line.setReadOnly(True)
self.btn_edit.setStyleSheet('background-color: rgb(119, 120, 121);')
def edit(self):
self.line.setReadOnly(False)
self.btn_edit.setStyleSheet('background-color :white')
def validate(self):
self.statusBar.showMessage("Qline Value is {}".format(self.line.text()), 8000)
def protected_field(self):
if self.line.isReadOnly():
self.statusBar.showMessage("This field is protected", 2000)
I also suggest using a vertical layout instead of calling move
and resize
on each widget. Check out QVBoxLayout
.