[Qt-interest] Force enter to default button rather than textEdit?

Syam Krishnan syamcr at gmail.com
Thu Sep 15 03:24:24 CEST 2011


On 09/15/2011 05:30 AM, Israel Brewster wrote:
> I have a window with a QPlaintextEdit and a button (among other 
> things). The QPlainTextEdit gets the focus by default, which I want 
> (so you can just type in it). However, when return is pressed, rather 
> than entering a new line in the QPlainTextEdit, I want it to activate 
> the button (which is set to default). I have successfully gotten the 
> QPlainTextEdit to ignore the enter key by overriding the keyPressEvent 
> function, but even so the button isn't picking up on it. I would use a 
> QlineEdit, but I want to be able to display more text than the width 
> alone allows. How can I make this work? Thanks.

Try using QObject::installEventFilter().
You can install an event filter on the text edit to filter out the 
key-press event for enter key. When such an event happens, you can call 
the slot that's connected to your default button (so that you'll get the 
same behaviour as clicking on the button).


Syam



More information about the Qt-interest-old mailing list