简体   繁体   English

如果在 ReadOnly State 中单击 QlineEdit 并根据 QlineEdit 是否在 ReadOnly state 中更改按钮颜色,请通知我们

[英]Notify us if a QlineEdit is clicked while being in ReadOnly State and change a button Color depending if QlineEdit is in ReadOnly state or not

I have a Pyqt Widget containing 3 buttons, 1 QlineEdit and 1 statusbar.我有一个 Pyqt 小部件,其中包含 3 个按钮、1 个 QlineEdit 和 1 个状态栏。 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.其中一个按钮使 qlineedit 处于只读状态 state,另一个按钮使 qlineedit 处于只读状态 state,最后一个按钮在状态栏消息中显示 Qlineedit 的值。

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.我想构建一个在 qlineedit 处于只读状态 state 并被单击时触发的事件,这应该会在状态栏中向我们显示一条消息,表明该字段受到保护。

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.最后,我想构建另一个事件,当禁用每次 Qlineedit 只读 State 时更改按钮“编辑”的颜色。

    ```
    
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.我创建了信号“selectionChanged”以在每次使用 if 语句单击 Qlineedit object 时通知我们,但它没有考虑 if 语句,它始终触发信号。

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.对于颜色更改,我使编辑方法在单击时返回 boolean True 值,其他按钮返回 False,并创建了一个 if 语句,每次 Edit 方法返回 True 时都会更改按钮颜色,但问题是一旦 boolean 值为 False,按钮就不会恢复正常颜色。

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.无需为同一信号创建额外的插槽,您只需在切换readOnly设置的方法中更改按钮的颜色即可。 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.事实上,您实际上什至不需要为viewedit方法设置两个单独的按钮……您可以只使用一个按钮来关闭和打开readOnly

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.此外,状态栏消息的if statement不起作用,因为每次按下按钮时都需要执行代码,因此需要将其移入插槽方法。

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.我还建议使用垂直布局,而不是在每个小部件上调用moveresize Check out QVBoxLayout .查看QVBoxLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM