繁体   English   中英

PyQt5:使用信号和插槽通过QPushButton验证LineEdit

[英]PyQt5: Validate LineEdit through QPushButton using signals and slots

将QPushButton连接到PyQt5中的QMessageBox时遇到麻烦,因为与PyQt4相比似乎没有多少文档。 就目前情况而言,QmessageBox在主布局之前执行,我认为这是.self.exec_()吗?

但是,第二个也是主要的问题是有关连接两个小部件的问题。 我正在寻求实施某种形式的有效性检查; 即,当两个QLineEdit字段均包含文本时,则在单击“提交”时,表单应清除该字段,但是,如果在单击“提交”时将两个字段中的任何一个都留为空白,则Id喜欢打开QMessageBox 我不确定如何实现此功能,因为我不知道如何将textField和PushButton连接在一起。

from PyQt5.QtWidgets import QWidget, QLabel, QLineEdit,QSpinBox, 
QDoubleSpinBox, QComboBox, QRadioButton, QPushButton, QHBoxLayout, QVBoxLayout,
QTextEdit, QGridLayout, QApplication, QMessageBox

from PyQt5.QtCore import Qt, pyqtSlot
import csv

class Buttons (QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.Widgets()
        self.arrange()
        self.close()
        self.messageBox()
        self.setWindowTitle ("test")



    def Widgets(self):

        self.nameLabel = QLabel("Name: ")
        self.nameLineEdit = QLineEdit()
        self.surnameLabel = QLabel("Surname: ")
        self.surnameLineEdit = QLineEdit()


        self.button1 = QPushButton("submit")
        self.button2 = QPushButton("cancel")
        self.button1.setMaximumSize(150,20)
        self.button2.setMaximumSize(150,20)



    def arrange (self):

        nameField = QVBoxLayout()
        nameField.addWidget(self.nameLabel)
        nameField.addWidget(self.nameLineEdit)
        nameField.addWidget(self.surnameLabel)
        nameField.addWidget(self.surnameLineEdit)

        #QHBoxLayout for Buttons:
        buttonsLayout = QHBoxLayout()
        buttonsLayout.addWidget(self.button1)
        buttonsLayout.addWidget(self.button2)
        self.button1.setSizePolicy(10,10)
        self.button2.setSizePolicy(10,10)



        #Creating mainLayout:
        mainLayout = QVBoxLayout()
        mainLayout.addLayout(nameField)
        mainLayout.addLayout(buttonsLayout)

        self.setLayout(mainLayout)

    @pyqtSlot()
    def close(self):

        #close window
        self.button2.clicked.connect(app.quit)

    def clear(self):    
        pass
        #Clear textFields when button is clicked:   




    @pyqtSlot()
    def messageBox(self):
        self.message = QMessageBox()
        self.message.setText ("submit ERROR")
        self.message.setStandardButtons(QMessageBox.Ok)
        self.button1.clicked.connect(self.messageBox)

        self.message.exec_()

我尝试了几种不同的技术,但都没有成功

        self.connect(self.Button1, SIGNAL("clicked()"),self.clicked)
        def clicked(self):
        QMessageBox.about(self, "message!"(self.messageBox()))





if __name__ == "__main__":
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication (sys.argv)
    window = Buttons()
    window.show()
    sys.exit (app.exec_())

阅读您的程序并遵循执行路径。 window = Buttons()开始,这意味着Buttons.__init__方法将运行。 在这个运行Buttons.messageBox连接上述messageBox槽到clicked的信号button1 然后,它将打开消息框( self.message.exec_() )。 您的__init__方法最终完成,然后调用app.exec_() ,它实际上显示主GUI并启动Qt事件循环,该循环处理诸如单击按钮和重​​绘事件之类的事情。

至此,您的程序已经被告知显示消息框,并配置为在单击按钮时运行Buttons.messageBox 单击该按钮时,它将显示消息框,并为该按钮建立其他连接。 下次单击按钮时,将显示2个消息框!

解决方案是从messageBox插槽中分离出信号连接。 与其在__init__方法中调用self.messageBox()self.messageBox()在那里建立button1的信号连接。

class Buttons (QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.Widgets()
        self.arrange()
        self.close()        
        self.button1.clicked.connect(self.messageBox)
        self.setWindowTitle ("test")

    @pyqtSlot()
    def messageBox(self):
        self.message = QMessageBox()
        self.message.setText ("submit ERROR")
        self.message.setStandardButtons(QMessageBox.Ok)

        self.message.exec_()

暂无
暂无

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

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