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. QTimer wait interval time before executing code first time
Forum Updated to NodeBB v4.3 + New Features

QTimer wait interval time before executing code first time

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 6 Posters 1.6k 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.
  • Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Don't use WinAPI but Qt: QWidget::keyPressEvent() e.g. here: https://forum.qt.io/topic/114207/keypressevent

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    B 1 Reply Last reply
    4
    • Christian EhrlicherC Christian Ehrlicher

      Don't use WinAPI but Qt: QWidget::keyPressEvent() e.g. here: https://forum.qt.io/topic/114207/keypressevent

      B Offline
      B Offline
      BD9a
      wrote on last edited by
      #5

      @Christian-Ehrlicher said in QTimer wait interval time before executing code first time:

      QWidget::keyPressEvent()

      In this Loop to handle keys instead of GetAsyncKeyState? But why?

      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @BD9a said in QTimer wait interval time before executing code first time:

        But why?

        Because you will block the ui otherwise.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        B 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @BD9a said in QTimer wait interval time before executing code first time:

          But why?

          Because you will block the ui otherwise.

          B Offline
          B Offline
          BD9a
          wrote on last edited by
          #7

          @Christian-Ehrlicher I dont know what u want to do, but my UI is fine with this winapi. Im using Qt Quick if it does matter.

          1 Reply Last reply
          0
          • B BD9a

            I have QTimer started with 3000msec interval. Is there a way to make it execute code and then wait this interval instead of wait interval and execute?

            A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by
            #8

            @BD9a said in QTimer wait interval time before executing code first time:

            I have QTimer started with 3000msec interval. Is there a way to make it execute code and then wait this interval instead of wait interval and execute?

            Perhaps using standard C/C++ sleep(0 instead QTimer would work in your case ?

            B 1 Reply Last reply
            0
            • A Anonymous_Banned275

              @BD9a said in QTimer wait interval time before executing code first time:

              I have QTimer started with 3000msec interval. Is there a way to make it execute code and then wait this interval instead of wait interval and execute?

              Perhaps using standard C/C++ sleep(0 instead QTimer would work in your case ?

              B Offline
              B Offline
              BD9a
              wrote on last edited by BD9a
              #9

              @AnneRanch Sleep will freeze my gui. I will probably use another loop to control it.
              Or maybe there's another solution.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @BD9a said in QTimer wait interval time before executing code first time:

                Or maybe there's another solution.

                Yes, use the functions Qt provides to you as I explained above...

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                B 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  @BD9a said in QTimer wait interval time before executing code first time:

                  Or maybe there's another solution.

                  Yes, use the functions Qt provides to you as I explained above...

                  B Offline
                  B Offline
                  BD9a
                  wrote on last edited by
                  #11

                  @Christian-Ehrlicher what? with sleep only my gui can be frozen. My gui without sleep works fine...

                  mrjjM 1 Reply Last reply
                  0
                  • B BD9a

                    @Christian-Ehrlicher what? with sleep only my gui can be frozen. My gui without sleep works fine...

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @BD9a
                    Hi
                    No Mr Ehrlicher just want you to use the normal Qt keyboard handling routines to detect the
                    F3 key so we dont have to loop around GetAsyncKeyState to see if key was pressed.
                    and when event comes with, call the timers slots code and then start timer.

                    B 1 Reply Last reply
                    3
                    • mrjjM mrjj

                      @BD9a
                      Hi
                      No Mr Ehrlicher just want you to use the normal Qt keyboard handling routines to detect the
                      F3 key so we dont have to loop around GetAsyncKeyState to see if key was pressed.
                      and when event comes with, call the timers slots code and then start timer.

                      B Offline
                      B Offline
                      BD9a
                      wrote on last edited by
                      #13

                      @mrjj That sounds better, but im doing sth wrong definetly.

                      I'm using Qt Quick, to Class where those loops are I just included #include <QEvent> #include <QKeyEvent>

                      protected:
                          bool eventFilter(QObject* obj, QEvent* event);
                      
                      bool Loop::eventFilter(QObject *obj, QEvent *event)
                      {
                          if (event->type()==QEvent::KeyPress) {
                              QKeyEvent* key = static_cast<QKeyEvent*>(event);
                              if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
                                  qDebug() << "enter";
                              } else {
                                  return QObject::eventFilter(obj, event);
                              }
                              return true;
                          } else {
                              return QObject::eventFilter(obj, event);
                          }
                          return false;
                      }
                      

                      And now this eventFilter should be in loop? cuz it's not working in current state.

                      mrjjM J.HilkJ 2 Replies Last reply
                      0
                      • B BD9a

                        @mrjj That sounds better, but im doing sth wrong definetly.

                        I'm using Qt Quick, to Class where those loops are I just included #include <QEvent> #include <QKeyEvent>

                        protected:
                            bool eventFilter(QObject* obj, QEvent* event);
                        
                        bool Loop::eventFilter(QObject *obj, QEvent *event)
                        {
                            if (event->type()==QEvent::KeyPress) {
                                QKeyEvent* key = static_cast<QKeyEvent*>(event);
                                if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
                                    qDebug() << "enter";
                                } else {
                                    return QObject::eventFilter(obj, event);
                                }
                                return true;
                            } else {
                                return QObject::eventFilter(obj, event);
                            }
                            return false;
                        }
                        

                        And now this eventFilter should be in loop? cuz it's not working in current state.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @BD9a
                        Hi
                        So loop class is the handler of the filter
                        and you set this filter on the object where you will press F3 on ?
                        or on Application ?
                        https://doc.qt.io/qt-5/qobject.html#installEventFilter

                        1 Reply Last reply
                        1
                        • B BD9a

                          @mrjj That sounds better, but im doing sth wrong definetly.

                          I'm using Qt Quick, to Class where those loops are I just included #include <QEvent> #include <QKeyEvent>

                          protected:
                              bool eventFilter(QObject* obj, QEvent* event);
                          
                          bool Loop::eventFilter(QObject *obj, QEvent *event)
                          {
                              if (event->type()==QEvent::KeyPress) {
                                  QKeyEvent* key = static_cast<QKeyEvent*>(event);
                                  if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
                                      qDebug() << "enter";
                                  } else {
                                      return QObject::eventFilter(obj, event);
                                  }
                                  return true;
                              } else {
                                  return QObject::eventFilter(obj, event);
                              }
                              return false;
                          }
                          

                          And now this eventFilter should be in loop? cuz it's not working in current state.

                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by
                          #15

                          @BD9a Since, you Using QML, have you considered using Shortcut-QML-Type
                          https://doc.qt.io/qt-5/qml-qtquick-shortcut.html
                          ?


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          B 2 Replies Last reply
                          4
                          • J.HilkJ J.Hilk

                            @BD9a Since, you Using QML, have you considered using Shortcut-QML-Type
                            https://doc.qt.io/qt-5/qml-qtquick-shortcut.html
                            ?

                            B Offline
                            B Offline
                            BD9a
                            wrote on last edited by
                            #16

                            @J-Hilk wow, thanks. I will use it to call my func and then start the loop (probably). thanks

                            1 Reply Last reply
                            0
                            • J.HilkJ J.Hilk

                              @BD9a Since, you Using QML, have you considered using Shortcut-QML-Type
                              https://doc.qt.io/qt-5/qml-qtquick-shortcut.html
                              ?

                              B Offline
                              B Offline
                              BD9a
                              wrote on last edited by
                              #17

                              @J-Hilk I tested it, and it's good, but I want to use it even if my app is not in focus. Is there a way to do it this way?

                              J.HilkJ 1 Reply Last reply
                              0
                              • B BD9a

                                @J-Hilk I tested it, and it's good, but I want to use it even if my app is not in focus. Is there a way to do it this way?

                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by J.Hilk
                                #18

                                @BD9a No Qt way, you'll have to look into your OS specific apis and/or install very low level Keyboard hooks,
                                https://docs.microsoft.com/de-de/windows/win32/api/winuser/nf-winuser-setwindowshookexa?redirectedfrom=MSDN


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                1

                                • Login

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