Results 1 to 5 of 5

Thread: Avoid stopping the program on QPushButton pressed

  1. #1
    Join Date
    Mar 2013
    Posts
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Avoid stopping the program on QPushButton pressed

    Hi,

    I have to check if a button is pressed for N seconds.

    I set up a timer that i start on the click button event and I check the button status on the timer timeout.

    The program is frezzed, i suppose in the right way, till i release the button. The timeout event is cached only when the button is released.

    Any suggestion?

  2. #2
    Join Date
    Apr 2012
    Location
    Paris
    Posts
    11
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Avoid stopping the program on QPushButton pressed

    Use QTime class. Like this:

    Qt Code:
    1. QTime _startClick; //(Attribute in .h file)
    2.  
    3. void MainWindow::on_pushButton_pressed()
    4. {
    5. _startClick.start();
    6. }
    7.  
    8. void MainWindow::on_pushButton_released()
    9. {
    10. qDebug() << _startClick.elapsed();
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2013
    Posts
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Avoid stopping the program on QPushButton pressed

    This could be a solution but the user have to press the button and count mentally the time enlapsed while I want the action fire automatically after that time..

  4. #4
    Join Date
    Apr 2012
    Location
    Paris
    Posts
    11
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Avoid stopping the program on QPushButton pressed

    Ha oki, in this case use QTimer like you said.
    You can connect the timeout signal of timer in released signal of button.
    On the pressed event of the button you start the timer.

    Qt Code:
    1. // Attribute in .h file
    2. QTimer _timer;
    3.  
    4. // In the constructor for example
    5. connect(&this->_timer, SIGNAL(timeout()), this->ui->pushButton, SIGNAL(released()));
    6.  
    7. void MainWindow::on_pushButton_pressed()
    8. {
    9. _timer.start(1000);
    10. }
    11.  
    12. void MainWindow::on_pushButton_released()
    13. {
    14. qDebug() << "Button pressed while 1000 ms";
    15. _timer.stop();
    16. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Avoid stopping the program on QPushButton pressed

    That line 5 is probably not such a good idea. You usually don't emit abother object's signal, objects emit their own signals whenever they know it is appropriate.

    If the idea is to trigger an action only if the button is pressed for a certain time, then you setup the timer with single shot and desired delay, connect the action to its timeout signal and the button's pressed signal to the timer's start slot and the button's released signal to the timer's stop slot.

    If the button is released before then timer runs it it is simply stopped, if the timer runs out the action is triggered. if the button is released after that happens it will stop an already stopped timer, nothing happens.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 17th March 2012, 14:01
  2. Replies: 2
    Last Post: 26th October 2011, 16:41
  3. Replies: 2
    Last Post: 12th September 2011, 16:40
  4. Removing pressed effect from a QPushButton
    By Luc4 in forum Qt Programming
    Replies: 3
    Last Post: 14th August 2010, 12:43
  5. Delay Loop until a QPushButton pressed
    By ljshap in forum Newbie
    Replies: 6
    Last Post: 17th January 2009, 17:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.