[PyQt] Pressing enter in QLineEdit clicks QPushButton?

Sibylle Koczian Sibylle.Koczian at T-Online.de
Mon Jun 22 10:52:00 BST 2009


"Mads Ipsen" <mpi at comxnet.dk> schrieb:
> Sibylle Koczian wrote:
> > Hello,
> >
> > I've got a dialog with a QLineEdit and a QPushButton. The QLineEdit is
> > connected to a method which takes its text and appends it to a
> > QTextBrowser (that's taken from the first GUI example in the PyQt book
> > by M. Summerfield). The "clicked()" signal of the QPushButton is
> > connected to a method that just prints a message. The problem: this
> > method, which should only be called by clicking the button, is called
> > every time Enter is pressed in the QLineEdit. This happens even if the
> > edit control isn't connected to any method.
> >
> > The button is next to the line edit in the form and in the tab order.
> >
> > With "setAutoDefault(false)" for this button and for the exit button
> > which follows in tab order I can prevent this behavior, but that isn't
> > really very comfortable. And it shouldn't be necessary, should it?
> >
> > All this on openSUSE with Python 2.6, PyQt 4.4.4.
> >
> > Thank you for help,
> > Sibylle
> >
> >   
> Can you supply a simple example that reproduces the behavior?
> 

Here it is. Tried with PyQt 4.4.4 (openSUSE) and PyQt 4.5.1 (Windows XP
Prof.), same behavior.

If the returnPressed() signal of the line edit is connected to the
logText() method, then pressing Enter calls logText() (as it should) and
then logPush() (as it shouldn't), pressing Tab doesn't call either of
them.

If editingFinished() is connected to logText() instead (commented out
here), then pressing Enter calls both methods, as before, pressing Tab
only calls logText(), but clicking on one of the buttons (self.pushtest
or btFertig) calls first logText() and then the method the button is
connected to and should call.

So in both cases the application doesn't do what I want it to do: put
the line edit text into the text browser if and only if enter (or tab)
is pressed after entering something in that control, and calling the
method connected to the button if and only if the button is pressed. 

What can I do, or what did I misunderstand?

Regards
Sibylle


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# test_lineeditenter.py

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Form(QDialog):
    
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.lineedit = QLineEdit(u"Write something and press Enter")
        self.pushtest = QPushButton(u"Test button")
        self.log = QTextBrowser()
        btFertig = QPushButton(u"E&xit")
        layout = QVBoxLayout()
        layout.addWidget(self.lineedit)
        layout.addWidget(self.pushtest)
        layout.addWidget(self.log)
        layout.addWidget(btFertig)
        self.setLayout(layout)
        self.lineedit.selectAll()
        self.lineedit.setFocus()
        self.connect(self.lineedit, SIGNAL("returnPressed()"), 
                     self.logText)
#        self.connect(self.lineedit, SIGNAL("editingFinished()"), 
#                     self.logText)
        self.connect(self.pushtest, SIGNAL("clicked()"), self.logPush)
        self.connect(btFertig, SIGNAL("clicked()"), self,
SLOT("close()"))
        self.setWindowTitle(u"Line edit problem")
    
    def logText(self):
        tx = self.lineedit.text()
        self.log.append(tx)
        self.lineedit.selectAll()
        self.lineedit.setFocus()
    
    def logPush(self):
        self.log.append("Button pressed")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mf = Form()
    mf.show()
    app.exec_()



-- 
Dr. Sibylle Koczian 



More information about the PyQt mailing list