How to use class QTimer ?



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





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



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



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



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



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


Log in to reply
 

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