QLialE中的QLineEdit小部件的奇怪行为,绑定按钮函数

时间:2017-08-09 07:43:40

标签: python pyqt pyqt4

我是pyqt编程的新手,所以也许我对此错了,但我发现在Enter上的self.lineEdit按下调用按钮的功能,即使在评论self.handleKeyRelease时也是如此。这是代码的最小值,也是我发现这种奇怪行为的第一个地方。

self.lineEdit = QtGui.QLineEdit(self.frame)

# self.lineEdit.keyReleaseEvent = self.handleKeyRelease

self.pushButton = QtGui.QPushButton(self.frame)
self.pushButton.clicked.connect(lambda event: self._insertIntoCategories(db, self.lineEdit))

这个相同行为的第二个地方是动态制作的形状小部件(连续有一个LineEdit和3个按钮)

        self.lineEdit_2 = QtGui.QLineEdit(self.frame_3)
        self.lineEdit_2.setText(item[1])    


        # ------------------------------------  block ------------------------------------------------- #

        self.pushButton_4 = QtGui.QPushButton(self.frame_3)       

        self.pushButton_4.clicked.connect(lambda event, id=item[0], le=self.lineEdit_2: self.modal('edit', 'Are you sure you want to rename this category ?', id, le))

        # ------------------------------------  block ------------------------------------------------- #

        self.pushButton_3 = QtGui.QPushButton(self.frame_3)

        self.pushButton_3.clicked.connect(lambda event, id=item[0]: self.__chIcon(id))


        # ------------------------------------  block ------------------------------------------------- #

        self.pushButton_2 = QtGui.QPushButton(self.frame_3)

        self.pushButton_2.clicked.connect(lambda event, id=item[0], le=self.lineEdit_2: self.modal('delete', 'Are you sure you want to delete this category ?', id, le))

每个按钮都可正常工作,但每次按Enter(或释放)上的每个条目都会从代码的前一部分调用函数(self.pushButton' s函数)self._insertIntoCategories如果此代码不够,如果需要,github上有完整的代码。

完全可执行的代码:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):

    def setupUi(self):
        self.Form = QtGui.QDialog()
        Form = self.Form
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(504, 550)
        self.verticalLayout = QtGui.QVBoxLayout(Form)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))

        self.frame = QtGui.QFrame(Form)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
        self.frame.setSizePolicy(sizePolicy)
        self.frame.setMinimumSize(QtCore.QSize(0, 50))
        self.frame.setStyleSheet(_fromUtf8("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0  green, stop:1 white);"))
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName(_fromUtf8("frame"))

        self.horizontalLayout_2 = QtGui.QHBoxLayout(self.frame)
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))

        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))

        # -------------- start of widget definitions -------------------------- #

        # THIS IS FIRTS INPUT #

        self.lineEdit = QtGui.QLineEdit(self.frame)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.horizontalLayout.addWidget(self.lineEdit)

        # AND THIS IS FIRST BUTTON

        self.pushButton = QtGui.QPushButton(self.frame)
        self.pushButton.setMinimumSize(QtCore.QSize(50, 0))
        self.pushButton.setStyleSheet(_fromUtf8("border-image: url(imgs/required/btns/add.png);background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 white, stop:1 white);"))
        self.pushButton.setText(_fromUtf8(""))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton.clicked.connect(lambda event: self.btnBehavior())
        self.horizontalLayout.addWidget(self.pushButton)

        # -------------- end of widget definitions -------------------------- #


        self.horizontalLayout_2.addLayout(self.horizontalLayout)

        self.verticalLayout.addWidget(self.frame)

        self.frame_2 = QtGui.QFrame(Form)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.frame_2.sizePolicy().hasHeightForWidth())
        self.frame_2.setSizePolicy(sizePolicy)
        self.frame_2.setStyleSheet(_fromUtf8("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 green, stop:1 white);"))
        self.frame_2.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_2.setObjectName(_fromUtf8("frame_2"))

        self.verticalLayout_2 = QtGui.QVBoxLayout(self.frame_2)
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))

        self.verticalLayout_3 = QtGui.QVBoxLayout()
        self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))

        self.frame_3 = QtGui.QFrame(self.frame_2)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.frame_3.sizePolicy().hasHeightForWidth())
        self.frame_3.setSizePolicy(sizePolicy)
        self.frame_3.setStyleSheet(_fromUtf8("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 white, stop:1 green);"))
        self.frame_3.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame_3.setFrameShadow(QtGui.QFrame.Raised)
        self.frame_3.setObjectName(_fromUtf8("frame_3"))

        self.horizontalLayout_3 = QtGui.QHBoxLayout(self.frame_3)
        self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))


        # -------------- start of widget definitions, second row -------------------------- #

        # THIS IS SECOND INPUT

        self.lineEdit_2 = QtGui.QLineEdit(self.frame_3)

        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.lineEdit_2.sizePolicy().hasHeightForWidth())
        self.lineEdit_2.setSizePolicy(sizePolicy)
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))

        self.horizontalLayout_3.addWidget(self.lineEdit_2)


        #THIS IS SECOND, THIRD AND FOURTH BUTTONS

        self.pushButton_4 = QtGui.QPushButton(self.frame_3)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.pushButton_4.sizePolicy().hasHeightForWidth())
        self.pushButton_4.setSizePolicy(sizePolicy)
        self.pushButton_4.setStyleSheet(_fromUtf8("border-image: url(imgs/required/btns/edit.jpg);background-color: transparent;"))
        self.pushButton_4.setText(_fromUtf8(""))
        self.pushButton_4.clicked.connect(lambda event: self.btnBehavior())
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))

        self.horizontalLayout_3.addWidget(self.pushButton_4)
        self.pushButton_3 = QtGui.QPushButton(self.frame_3)
        self.pushButton_3.setStyleSheet(_fromUtf8("border-image: url(imgs/required/btns/ch_pic.png); background-color: transparent;"))
        self.pushButton_3.setText(_fromUtf8(""))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_3.clicked.connect(lambda event: self.btnBehavior())
        self.horizontalLayout_3.addWidget(self.pushButton_3)

        self.pushButton_2 = QtGui.QPushButton(self.frame_3)
        self.pushButton_2.setStyleSheet(_fromUtf8("border-image: url(imgs/required/btns/trash.png);background-color: transparent;"))
        self.pushButton_2.setText(_fromUtf8(""))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.pushButton_2.clicked.connect(lambda event: self.btnBehavior())
        self.horizontalLayout_3.addWidget(self.pushButton_2)

        # -------------- end of widget definitions -------------------------- #

        self.verticalLayout_3.addWidget(self.frame_3)
        self.verticalLayout_2.addLayout(self.verticalLayout_3)

        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)

        self.verticalLayout_2.addItem(spacerItem)
        self.verticalLayout.addWidget(self.frame_2)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)


    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.pushButton_4.setToolTip(_translate("Form", "<html><head/><body><p>rename</p></body></html>", None))
        self.pushButton_3.setToolTip(_translate("Form", "<html><head/><body><p>picture</p></body></html>", None))
        self.pushButton_2.setToolTip(_translate("Form", "<html><head/><body><p>delete</p></body></html>", None))
        self.pushButton_2.setWhatsThis(_translate("Form", "<html><head/><body><p>delete</p></body></html>", None))

    def btnBehavior(self):
        print('btn clicked');


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    # Form = QtGui.QDialog()
    ui = Ui_Form()
    ui.setupUi()
    ui.Form.show()
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:3)

根据documentation

  

autoDefault:bool

     

此属性保持按钮是否为自动默认按钮

     

如果此属性设置为true,则按钮是自动的   默认按钮。

     

在某些GUI样式中,使用额外的框架绘制默认按钮   在它周围,最多3像素或更多。 Qt自动保留这个空间   免费的自动默认按钮,即自动默认按钮可能有   稍微大一点的提示。

     

对于具有QDialog的按钮,此属性的默认值为true   父母;否则默认为false。

     

有关default和auto-default的详细信息,请参阅default属性   交互。

也就是说,这是这个问题的负责任财产。解决方法是将<{>>所有QPushButtons 的class Dialog(QtGui.QDialog, Ui_Form): def __init__(self, parent=None): QtGui.QDialog.__init__(self, parent) self.setupUi(self) for pb in self.findChildren(QtGui.QPushButton): pb.setAutoDefault(False) self.pushButton.clicked.connect(self.btnBehavior) def btnBehavior(self): print('btn clicked') 属性设置为{{1}}。

为了以简单的方式做到这一点,我重构了你的代码,为了做到这一点,把Qt Designer的状态生成,然后实现了实现该设计的逻辑,因为这是我提出的解决方案:

{{1}}

正如@ekhumoro所说,你可以用Qt Designer做同样的事。,

enter image description here

答案 1 :(得分:1)

您使用QDialog作为表单的基础,我发现这个link海报注意到您做过同样的事情,您可以通过将基类更改为QWidget来修复行为,我没有深入研究它,但我的猜测是它与特定于QDialog的returnPressed()行为的预期用途有关。如果您不想使用QWidget,则可以尝试重新实现returnPressed()方法,更多关于此link的方法。