Results 1 to 4 of 4

Thread: Exit unending loop with Button

  1. #1
    Join Date
    Aug 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Exit unending loop with Button

    Good day.

    I have created a GUI that uses its buttons to change different states of a system. I want to be able to run a unending loop that reads data over the PCI bus inside of each event of a button. I have the buttons and I have the functions that run the unending loop that reads the PCI bus.
    However, when the loop is running inside the event of the button I loose functionality of the gui.

    So I basicly want to learn how to run a unending loop in the event of a button and still be able to control the GUI

    I will try to explain with code:

    Qt Code:
    1. void Controller::standby()
    2. {
    3. powerDownButton->setEnabled(true);
    4. standbyButton->setEnabled(false);
    5.  
    6. int val = 1;
    7. while (val == 1)
    8. {
    9. read_data();
    10.  
    11. //here I nead something to exit the loop when a button is pressed;
    12. //something that I can use to change val to another value and exit the loop
    13. }
    To copy to clipboard, switch view to plain text mode 

    Hope this post makes sense.
    Thanks to anyone that can help!

  2. #2
    Join Date
    Sep 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Exit unending loop with Button

    You have to create a new thread (with QThread) and place in it this unending loop.
    See also this.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Exit unending loop with Button

    or you use QCoreApplication::processEvents(). something like this:
    Qt Code:
    1. void Controller::on_powerDownButton_clicked()
    2. {
    3. m_buttonPressed = true;
    4. }
    5.  
    6. void Controller::standby()
    7. {
    8. powerDownButton->setEnabled(true);
    9. standbyButton->setEnabled(false);
    10. int val = 1;
    11. m_buttonPressed = false; // member variable!
    12. while (val == 1)
    13. {
    14. read_data();
    15. QCoreApplication::processEvents();
    16. if (m_buttonPressed)
    17. {
    18. //do your stuff here...
    19. }
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    depends on your task what is better. This or a separate thread.

  4. #4
    Join Date
    Aug 2009
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Exit unending loop with Button

    Thanx Lykurg!!!!

    I was afraid I had to do mutlithreading, but this is the solution I was looking for.

    I appreciate the help!

Similar Threads

  1. Exit button that does a task before exiting
    By Roelof in forum Qt Programming
    Replies: 5
    Last Post: 11th September 2009, 14:21
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. Window without a exit button
    By jbpvr in forum Qt Programming
    Replies: 4
    Last Post: 12th March 2007, 23:54
  4. While statement inside the main loop
    By OnionRingOfDoom in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2006, 18:17

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.