K.Mulier K.Mulier - 4 months ago 55
Python Question

How to bring keyboard focus to QTextEdit in PyQt?

I've inserted a simple

QTextEdit
widget into my PyQt user interface. When the user wants to type text into that widget, he has to click on it. My program should be able to make this happen automatically at certain occasions, such that the user can start typing text into that
QTextEdit
widget without the need for clicking on it.

I already got somewhere, but the issue is still not solved completely. When my program calls the
focus()
function, the cursor will start blinking at the end of the last line. But typing on your keyboard doesn't insert any text.

class myTextField(QPlainTextEdit):

def __init__(self):
super(myTextField, self).__init__()
...


def focus(self):
self.focusInEvent(QFocusEvent( QEvent.FocusIn ))
# Now the cursor blinks at the end of the last line.
# But typing on your keyboard doesn't insert any text.
# You still got to click explicitly onto the widget..

...

###


Any help is greatly appreciated :-)

Answer

Use setFocus() method.

def focus(self):
    self.setFocus()
Comments