How assign the enter key to a button



  • 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



  • 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.



  • [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.



  • 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 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?



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

    Thanks



  • 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]



  • So, what should I be using?

    Thanks



  • An event filter on the window would do the trick.



  • 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



  • 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.



  • 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



  • 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.



  • 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!



  • 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



  • 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



  • @void MainWindow::keyPressEvent(QKeyEvent* pe)
    {
    if(pe->key() == Qt::Key_Return) yout_button_slot();
    }@

    Sorry but I'm lost now. Where do I place this function?



  • 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_OBJECT

    public:
    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



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.