April 9, 2012

fs_tigre fs_tigre
Ant Farmer
146 posts

How assign the enter key to a button

Page  
1

Hi,

How can I assign the enter key to a button? In other words how can I assign the enter key to a button so that when the enter key is pressed it basically trigers the button.

Thanks

18 replies

April 9, 2012

adnan adnan
Hobby Entomologist
269 posts

There is an option for setDefault(bool) . If you are using Qt creater, just select the form( your_app.ui). When the form appears select the button and enable setDefault. Alternately,

  1. your_push_button->setDefault(true)

This will work if your dialog or window has focus at the time of pressing enter.

April 9, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

fs_tigre wrote:
Hi,

How can I assign the enter key to a button? In other words how can I assign the enter key to a button so that when the enter key is pressed it basically trigers the button.

Thanks

If your Button is on QDialog or its subClass, you can using setDefault(), otherwise, maybe you can use eventfilter.

April 10, 2012

fs_tigre fs_tigre
Ant Farmer
146 posts

Hmm, setting the button to default didn’t work.

This is what I tried…

  1. ui->pushButton_Calculate->setDefault(true);

Any other suggestion?

Thank you all for your help

April 10, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

fs_tigre wrote:
Hmm, setting the button to default didn’t work.

This is what I tried…

  1. ui->pushButton_Calculate->setDefault(true);

Any other suggestion?

Thank you all for your help

Are you sure your button is on a QDialog?

April 10, 2012

fs_tigre fs_tigre
Ant Farmer
146 posts

No, this is in a window, should it be different?

Thanks

April 10, 2012

1+1=2 1+1=2
Hobby Entomologist
309 posts

Yes, indeed, it’s QDialog’s feature.

fs_tigre wrote:
No, this is in a window, should it be different?

Thanks

April 10, 2012

fs_tigre fs_tigre
Ant Farmer
146 posts

So, what should I be using?

Thanks

April 10, 2012

Andre Andre
Area 51 Engineer
6031 posts

An event filter on the window would do the trick.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 10, 2012

fs_tigre fs_tigre
Ant Farmer
146 posts

Thank you all.

How do you apply the event filter to a button?

I tried this but it dint work.

  1. ui->pushButton_Calculate->eventFilter(pushButton,true);

Thanks

April 10, 2012

Andre Andre
Area 51 Engineer
6031 posts

No, that does not work. Did you read the documenation on event filters? You need to install it on the top-level widget, subclass your Button, and reimplement the eventFilter method.

Alternatively, and easier: you can use a QShortCut. Just create a shortcut for the Enter key, and connect it’s activated() signal to the click() slot of the button.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 10, 2012

fs_tigre fs_tigre
Ant Farmer
146 posts

No, that does not work. Did you read the documenation on event filters? You need to install it on the top-level widget, subclass your Button, and reimplement the eventFilter method.

I actually did, the problem is that I’m a beginner on Qt as well as on C++, so I don’t completely understand the documentation. In fact I’m having a hard time following your instructions… Sorry!!!
bq. You need to install it on the top-level widget, subclass your Button, and reimplement the eventFilter method.

Too advanced. I know I know, its probably a pain to teach a beginner. I will reread and try again.

Thanks a lot for your help

April 10, 2012

Andre Andre
Area 51 Engineer
6031 posts

Why don’t you try try the QShortCut method then? That should be easier…

Having a basic understanding of C++, including classes & objects, inheritance and polymorphism is really needed to work with Qt. If you don’t understand those, then please get a C++ book and learn about them. It will make it much easier for you to understand Qt.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 10, 2012

fs_tigre fs_tigre
Ant Farmer
146 posts

Why don’t you try try the QShortCut method then? That should be easier…
Having a basic understanding of C++, including classes & objects, inheritance and polymorphism is really needed to work with Qt. If you don’t understand those, then please get a C++ book and learn about them. It will make it much easier for you to understand Qt.

I will try the short cut. As far as learning C++, I’m currently taking a class and reading as much as I can.

Thanks a lot for your help!

April 11, 2012

fs_tigre fs_tigre
Ant Farmer
146 posts

I tried the QShortcut but I cannot make it work.

Here is my code, I dont get any errors but it doen’t assign the enter key to my button

  1.  QShortcut *shortcut = new QShortcut(QKeySequence("Enter"), ui->groupBox);
  2. QObject::connect(shortcut, SIGNAL(activated()), ui->pushButton_Calculate, SLOT(click()));

I also tried…
  1. QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_Enter), ui->groupBox);
  2. QObject::connect(shortcut, SIGNAL(activated()), ui->pushButton_Calculate, SLOT(click()));

But it didn’t work.

Any idea why this is not working?

thanks

April 11, 2012

RaubTieR RaubTieR
Lab Rat
42 posts

Also you can reimplement this for your window

  1. void MainWindow::keyPressEvent(QKeyEvent* pe)
  2. {
  3.  if(pe->key() == Qt::Key_Return) yout_button_slot();
  4. }

this function is virtual and present in QMainWindow’s base class, though may be you will need

  1. #include <QKeyEvent>

you may also check for Key_Enter which is actually numpad one

Page  
1

  ‹‹ Problem with Context Menu      Drawing multiple rectangles in Qt ››

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