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 use class QTimer ?
QtWS25 Last Chance

How to use class QTimer ?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 7.0k Views
  • 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.
  • M Offline
    M Offline
    master huang
    wrote on last edited by
    #1

    As the question of Title, I want to delete all of the phone numbers when I press the mouse and not release,But I don't know how to use the class QTimer,If you can solve it ,please help me , thanks~~

    1 Reply Last reply
    0
    • L Offline
      L Offline
      leon.anavi
      wrote on last edited by
      #2

      Please check "QTimer documentation":http://qt-project.org/doc/qt-4.8/QTimer.html and "this example":http://www.developer.nokia.com/Community/Wiki/How_to_use_QTimer_in_Qt.

      http://anavi.org/

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Scylla
        wrote on last edited by
        #3

        I don't get it. How is the mouse butten press event related to a QTimer?
        Can you explain what do want to do?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          leon.anavi
          wrote on last edited by
          #4

          [quote author="Scylla" date="1331795560"]I don't get it. How is the mouse butten press event related to a QTimer?
          Can you explain what do want to do?[/quote]

          I guess that he wants to start a timer on mouse press event.

          To catch a mouse press event you have to override the virtual method "mousePressEvent":http://qt-project.org/doc/qt-4.8/qwidget.html#mousePressEvent at your widget which inherits QWidget.

          http://anavi.org/

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

            I guess he wants to distinguish between a long and a short mouse press, but I'm just guessing here.

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on last edited by
              #6

              Yep, he probably wants to start the timer on mouse press and stop it on release and react based on how long was the press event.

              I have done something similar, but including the distance travelled during the press event as well in order to distinguish selecting scene items (click) from panning the scene (move) from moving scene items (long click and then move). There was this issue with the way graphics scene/item stack works which caused a lot of unintended actions.

              Note that for this purpose, you should use a QElapsedTimer, which is designed to tell you ... well, how much time has elapsed, whereas QTimer is designed to run some slot after a set amount of time.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                master huang
                wrote on last edited by
                #7

                [quote author="ddriver" date="1331802145"]Yep, he probably wants to start the timer on mouse press and stop it on release and react based on how long was the press event.

                I have done something similar, but including the distance travelled during the press event as well in order to distinguish selecting scene items (click) from panning the scene (move) from moving scene items (long click and then move). There was this issue with the way graphics scene/item stack works which caused a lot of unintended actions.

                Note that for this purpose, you should use a QElapsedTimer, which is designed to tell you ... well, how much time has elapsed, whereas QTimer is designed to run some slot after a set amount of time.[/quote]

                oh ,I just use the class QTimer ,such as : QTimer *timer = new QTimer(this);
                connect(timer,SIGNAL(timeout()),this,SLOT(my slot()));
                timer->start(1000);
                it means that,one second later ,the function my slot() will be execute,but I don't konw what to do later?

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  Well, in that case you simply put the code you want to get executed in that slot, the slot is just a regular function with the added benefit of being invokable through signals. What you want to do later should be in the body of the slot the QTimer is connected to - in your case - delete all the phone numbers. As simple as that.

                  @connect(timer, SIGNAL(timeout()), this, SLOT(deleteAllPhoneNumbers()));@

                  You just need to put the deletion code in the implementation of the deleteAllPhoneNumbers() slot or whatever you want to call it.

                  You also have to connect your button press event to the timer to start it.

                  @connect(deleteButton, SIGNAL(pressed()), timer, SLOT(start()));@

                  So you have the button triggering the timer triggering the function to delete phone numbers. Button -> Timer -> Slot

                  In short, you don't have to mess with the press event of the button itself, as by default it will emit a pressed signal, and that is all you really need. You only have to do it IF you want to use your widget instead of a button. But I don't really see what purpose could a widget have if it deletes everything on every mouse press.


                  On a side note, in case you want to do what I described in my previous post, you will probably have to use a combination of QTimer and QElapsedTimer, QTimer you will need to time periodical checks on how much time has elapsed since the press event, check the distance the press event travelled and decide the appropriate user interaction.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    master huang
                    wrote on last edited by
                    #9

                    [quote author="leon.anavi" date="1331795093"]Please check "QTimer documentation":http://qt-project.org/doc/qt-4.8/QTimer.html and "this example":http://www.developer.nokia.com/Community/Wiki/How_to_use_QTimer_in_Qt.[/quote]

                    ok ,thanks for your help ,you help so much~~

                    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