October 7, 2011

pratik041 pratik041
Lab Rat
346 posts

how we can set enter key to a widget so that while pressing enter a click event should occur?

 

the widget should respond to enter key.

 Signature 

Pratik Agrawal

6 replies

October 7, 2011

Gerolf Gerolf
Robot Herder
3286 posts

Are you talking from standard or custom widgets?

You could subclass the widget, overwrite the keyPressEvent and do it there.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

October 7, 2011

pratik041 pratik041
Lab Rat
346 posts

but inside key press event does i have to use Qobject::connect to make the enter to respond or some other way can you give example?

 Signature 

Pratik Agrawal

October 7, 2011

fluca1978 fluca1978
Lab Rat
529 posts

I guess subclassing you have to intercept the enter key and manually call the function that implements the slot “clicked” in your button, or “triggered” if it is an action, and so on.

October 7, 2011

Gerolf Gerolf
Robot Herder
3286 posts

I’m a bit confused.
If you have for example a QPushButton and want it to emit a clicked signal in case a B key is pressed, you overwrite keyPressEvent, make an if to check for the B and then emit the needed signal.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

October 7, 2011

pratik041 pratik041
Lab Rat
346 posts

ok thank’s

 Signature 

Pratik Agrawal

October 7, 2011

Andre Andre
Robot Herder
6417 posts

You can just make your button the default button on the form.

  1. QButton::setDefault(true);

That will make it respond to an Enter click anywhere in the form, except when the currently focussed widget accepts the enter key itself (like a QPlainTextEdit widget).

 
  ‹‹ [SOLVED]Get mainwindows Widgets      Showing my mainwindow from a controller class ››

You must log in to post a reply. Not a member yet? Register here!