How assign the enter key to a button
-
wrote on 9 Apr 2012, 19:19 last edited by
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
-
wrote on 9 Apr 2012, 19:45 last edited by
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,
@your_push_button->setDefault(true)@This will work if your dialog or window has focus at the time of pressing enter.
-
wrote on 9 Apr 2012, 20:05 last edited by
[quote author="fs_tigre" date="1333999164"]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[/quote]
If your Button is on QDialog or its subClass, you can using setDefault(), otherwise, maybe you can use eventfilter.
-
wrote on 9 Apr 2012, 22:35 last edited by
Hmm, setting the button to default didn't work.
This is what I tried...
@ui->pushButton_Calculate->setDefault(true);@Any other suggestion?
Thank you all for your help
-
wrote on 9 Apr 2012, 22:40 last edited by
[quote author="fs_tigre" date="1334010901"]Hmm, setting the button to default didn't work.
This is what I tried...
@ui->pushButton_Calculate->setDefault(true);@Any other suggestion?
Thank you all for your help
[/quote]
Are you sure your button is on a QDialog?
-
wrote on 9 Apr 2012, 23:00 last edited by
No, this is in a window, should it be different?
Thanks
-
wrote on 9 Apr 2012, 23:13 last edited by
Yes, indeed, it's QDialog's feature.
[quote author="fs_tigre" date="1334012435"]No, this is in a window, should it be different?
Thanks[/quote]
-
wrote on 9 Apr 2012, 23:24 last edited by
So, what should I be using?
Thanks
-
wrote on 10 Apr 2012, 07:43 last edited by
An event filter on the window would do the trick.
-
wrote on 10 Apr 2012, 13:22 last edited by
Thank you all.
How do you apply the event filter to a button?
I tried this but it dint work.
@ui->pushButton_Calculate->eventFilter(pushButton,true);@Thanks
-
wrote on 10 Apr 2012, 13:51 last edited by
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 [[doc:QShortCut]]. Just create a shortcut for the Enter key, and connect it's activated() signal to the click() slot of the button.
-
wrote on 10 Apr 2012, 14:02 last edited by
bq. 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
-
wrote on 10 Apr 2012, 14:05 last edited by
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.
-
wrote on 10 Apr 2012, 14:26 last edited by
bq. 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!
-
wrote on 10 Apr 2012, 23:38 last edited by
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
@ QShortcut *shortcut = new QShortcut(QKeySequence("Enter"), ui->groupBox);
QObject::connect(shortcut, SIGNAL(activated()), ui->pushButton_Calculate, SLOT(click()));@
I also tried...
@QShortcut *shortcut = new QShortcut(QKeySequence(Qt::Key_Enter), ui->groupBox);
QObject::connect(shortcut, SIGNAL(activated()), ui->pushButton_Calculate, SLOT(click()));@
But it didn't work.Any idea why this is not working?
thanks
-
wrote on 11 Apr 2012, 00:19 last edited by
Also you can reimplement this for your window
@void MainWindow::keyPressEvent(QKeyEvent* pe)
{
if(pe->key() == Qt::Key_Return) yout_button_slot();
}@this function is virtual and present in QMainWindow's base class, though may be you will need
@#include <QKeyEvent>@you may also check for Key_Enter which is actually numpad one
-
wrote on 11 Apr 2012, 01:57 last edited by
well you've told you have a window with buttons, this window has a specific class, which might be declared like that for instance (by default for single window gui app) :
@class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private:
Ui::MainWindow *ui;// here i add this function
protected:
void keyPressEvent(QKeyEvent* pe);
};@in protected section you declare the function, you may define it here or in the corresponding .cpp file - as usual method
-
wrote on 11 Apr 2012, 11:39 last edited by
I found my problem, I had my code in the wrong place.
This is the code that worked.
@QShortcut *returnShortcut = new QShortcut(QKeySequence("Return"), ui->groupBox);
QObject::connect(returnShortcut, SIGNAL(activated()), ui->pushButton_Calculate, SLOT(click()));@Thank you all very much for your help!
-
I found my problem, I had my code in the wrong place.
This is the code that worked.
@QShortcut *returnShortcut = new QShortcut(QKeySequence("Return"), ui->groupBox);
QObject::connect(returnShortcut, SIGNAL(activated()), ui->pushButton_Calculate, SLOT(click()));@Thank you all very much for your help!