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 to show/hide application with shortcut?
Qt 6.11 is out! See what's new in the release blog

How to show/hide application with shortcut?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 12.4k Views 1 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.
  • A Offline
    A Offline
    absfrm
    wrote on last edited by
    #1

    hi
    I'm using windows
    how can i show or hide my application by press shortcut key?
    "such as some application that show with F11 or ..."

    If You Want You Can!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      Qt Developers usually use "QShortcut":http://qt-project.org/doc/qt-5.0/qshortcut.html class. It allows to set key sequence on slot.

      Example use:
      @
      QShortcut * shortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")), parent);
      connect(shortcut, SIGNAL(activated()), this, SLOT(openFile()));
      @

      It's easy as 1, 2, 3.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AcerExtensa
        wrote on last edited by
        #3

        Simply use WINAPI function: RegisterHotKey -> http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx

        God is Real unless explicitly declared as Integer.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tucnak
          wrote on last edited by
          #4

          [quote author="AcerExtensa" date="1340617362"]Simply use WINAPI function: RegisterHotKey -> http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309(v=vs.85).aspx[/quote]

          Why WINAPI? There is QShortcut which allows to do it on cross-platform level.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            AcerExtensa
            wrote on last edited by
            #5

            tucnak variant works for application-wide association, RegisterHotKey - is system wide. Have thought you are searching for system wide association...

            God is Real unless explicitly declared as Integer.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              absfrm
              wrote on last edited by
              #6

              i used this code
              @
              QShortcut * shwHide = new QShortcut(QKeySequence("Ctrl+Space"), this);
              QObject::connect(shwHide, SIGNAL(activated()), this, SLOT(showhide()));
              ///
              void MainWindow::showhide(){
              if (isVisible()){
              hide();
              }
              else{
              show();
              }
              }
              @

              what is Difference?

              If You Want You Can!

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tucnak
                wrote on last edited by
                #7

                [quote author="AcerExtensa" date="1340617534"]tucnak variant works for application-wide association, RegisterHotKey - is system wide. Have thought you are searching for system wide association...[/quote]

                I can't understand what are you talking about. WINAPI working well on Windows, but if author want to extend it for Linux, he will add some X11 functions. But Qt has QShortcut class which allow to do it on the high-level.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tucnak
                  wrote on last edited by
                  #8

                  [quote author="abbas farahmand" date="1340617665"]i used this code
                  @
                  QShortcut * shwHide = new QShortcut(QKeySequence("Ctrl+Space"), this);
                  QObject::connect(shwHide, SIGNAL(activated()), this, SLOT(showhide()));
                  ///
                  void MainWindow::showhide(){
                  if (isVisible()){
                  hide();
                  }
                  else{
                  show();
                  }
                  }
                  @

                  what is Difference? [/quote]

                  Your code looks clear. Try to change it to it:
                  @
                  void MainWindow::showhide(){
                  qDebug() << "Ctrl+Space called.';
                  }
                  @

                  and check Application Output on execution.

                  P.S. In other way you can implement QWidget::keyPressEvent(QKeyEvent*):
                  @
                  void MainWindow::keyPressEvent(QKeyEvent *e) {
                  if(e->key() == Qt::Key_Space && e->modifiers() == Qt::CtrlModifier) {
                  qDebug() << "called";
                  return;
                  }
                  QWidget::keyPressEvent(e);
                  }
                  @

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

                    bq. I can’t understand what are you talking about. WINAPI working well on Windows

                    1. platform is declared exactly.

                    bq. I’m using windows

                    1. Like I've said, I thought he need systemwide shortcut for his application. QShortcut will work only if application is activated(focused), RegisterHotKey can associate systemwide shortcut for application(like yakuake in linux or whatever). I was wrong, I'm sorry, forgive me peoples. sarcasm

                    Can you understand it now?

                    God is Real unless explicitly declared as Integer.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tucnak
                      wrote on last edited by
                      #10

                      [quote author="AcerExtensa" date="1340618195"]bq. I can’t understand what are you talking about. WINAPI working well on Windows

                      1. platform is declared exactly.

                      bq. I’m using windows

                      1. Like I've said, I thought he need systemwide shortcut for his application. QShortcut will work only if application is activated(focused), RegisterHotKey can associate systemwide shortcut for application(like yakuake in linux or whatever). I was wrong, I'm sorry, forgive me peoples. sarcasm

                      Can you understand it now? [/quote]

                      I've understood. But I think that author want application-wide shortcut (like F11), so QShortcut will be better.

                      And I said about platform too. He can begin to want to build it on Linux for example.

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

                        bq. But I think that author want application-wide shortcut (like F11), so QShortcut will be better.

                        Whats right, after authors second post I see it too, what right choice for him is QShortcut :)

                        bq. And I said about platform too. He can begin to want to build it on Linux for example.

                        He can begin to want to capture the universe! Who knows? What we will do then? :) The platform was specified. :)

                        Lets stop this discussion :)

                        God is Real unless explicitly declared as Integer.

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          tucnak
                          wrote on last edited by
                          #12

                          [quote author="AcerExtensa" date="1340618982"]bq. But I think that author want application-wide shortcut (like F11), so QShortcut will be better.

                          Whats right, after authors second post I see it too, what right choice for him is QShortcut :)

                          bq. And I said about platform too. He can begin to want to build it on Linux for example.

                          He can begin to want to capture the universe! Who knows? What we will do then? :) The platform was specified. :)

                          Lets stop this discussion :)[/quote]

                          OK. Thanks for discussion. Good luck.

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

                            [quote author="abbas farahmand" date="1340617665"]
                            Your code looks clear. Try to change it to it:
                            @
                            void MainWindow::showhide(){
                            qDebug() << "Ctrl+Space called.';
                            }
                            @

                            and check Application Output on execution.
                            [/quote]

                            i changed it,but how can i check Application Output on execution?(sorry I'm not professional in qt)

                            If You Want You Can!

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              tucnak
                              wrote on last edited by
                              #14

                              [quote author="abbas farahmand" date="1340619266"][quote author="abbas farahmand" date="1340617665"]
                              Your code looks clear. Try to change it to it:
                              @
                              void MainWindow::showhide(){
                              qDebug() << "Ctrl+Space called.';
                              }
                              @

                              and check Application Output on execution.
                              [/quote]

                              i changed it,but how can i check Application Output on execution?(sorry I'm not professional in qt)[/quote]

                              You should Run (Ctrl+R) your application in Qt Creator and open in Qt Creator tab "Application Output". There will be all messages which your application prints with cout, cerr, qDebug(), qWarning(), etc.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                absfrm
                                wrote on last edited by
                                #15

                                thanks for your reply.
                                good luck.

                                If You Want You Can!

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  tucnak
                                  wrote on last edited by
                                  #16

                                  You are welcome. Good luck and happy coding!

                                  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