Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How assign the enter key to a button
Forum Updated to NodeBB v4.3 + New Features

How assign the enter key to a button

Scheduled Pinned Locked Moved General and Desktop
20 Posts 6 Posters 31.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dbzhang800
    wrote on last edited by
    #7

    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]

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fs_tigre
      wrote on last edited by
      #8

      So, what should I be using?

      Thanks

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #9

        An event filter on the window would do the trick.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fs_tigre
          wrote on last edited by
          #10

          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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #11

            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.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fs_tigre
              wrote on last edited by
              #12

              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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #13

                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.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fs_tigre
                  wrote on last edited by
                  #14

                  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!

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    fs_tigre
                    wrote on last edited by
                    #15

                    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

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      RaubTieR
                      wrote on last edited by
                      #16

                      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

                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        fs_tigre
                        wrote on last edited by
                        #17

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

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          RaubTieR
                          wrote on last edited by
                          #18

                          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

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            fs_tigre
                            wrote on last edited by
                            #19

                            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!

                            C 1 Reply Last reply
                            0
                            • F fs_tigre

                              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!

                              C Offline
                              C Offline
                              crates
                              wrote on last edited by
                              #20

                              @fs_tigre great!!

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved