Results 1 to 6 of 6

Thread: Button-Controlled Loop

  1. #1
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Button-Controlled Loop

    Hi. I've encountered trouble fixing my code since last night. It's just a simple program that has an infinite loop and I'm trying to modify the bool sentinel using buttons. I don't know what I'm missing because I keep getting errors on build. I hope you can help me out.
    Attached Files Attached Files

  2. #2
    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: Button-Controlled Loop

    Once you start the loop, the Gui isn't accessible so you couldn't stop the loop. Two solutions: use QCoreApplication::processEvents() or use a thread for the loop. You also can read this article.

  3. #3
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Button-Controlled Loop

    I was working on some other things I left out this part of my program. Anyway, I'm not familiar with the processEvents() can you help me out in a little more detail? Where and how do I implement this? I ended up with this--a lot of errors in my looper.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "looper.h"
    3.  
    4. bool stop;
    5. void mainLoop();
    6.  
    7. basicLoop::basicLoop(QWidget *parent)
    8. {
    9. setupUi(this); // this sets up GUI
    10.  
    11. connect( startButton, SIGNAL( clicked() ), this, SLOT( startLoop() ) );
    12. connect( stopButton, SIGNAL( clicked() ), this, SLOT( stopLoop() ) );
    13. }
    14.  
    15. void basicLoop::startLoop()
    16. {
    17. &stop = false;
    18. mainLoop();
    19. }
    20.  
    21. void basicLoop::stopLoop()
    22. {
    23. &stop = true;
    24. }
    25.  
    26. void mainLoop()
    27. {
    28. int i=0;
    29. //check for stop button press
    30. QCoreApplication::processEvents();
    31. if (stopButton->isChecked) stopLoop();
    32. while(!&stop)
    33. {
    34. textEdit->append(QString::number(i));
    35. i++;
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    I'm not sure if this is making sense..I don't know how to connect my stopButton now and how I would call the function stopLoop..

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Button-Controlled Loop

    this is how its done.

    Qt Code:
    1. void mainLoop()
    2. {
    3. int i=0;
    4. //check for stop button press
    5. //QCoreApplication::processEvents();//Put this inside loop
    6. //if (stopButton->isChecked) stopLoop();//no need for this
    7. while(!&stop)
    8. {
    9. QCoreApplication::processEvents();
    10. textEdit->append(QString::number(i));
    11. i++;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2009
    Posts
    55
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Button-Controlled Loop

    thanks for that..I'm gettin close
    just a minor problem here though..I couldn't access the textEdit object since my function mainLoop is not part of the widget basicLoop..how do I access to textEdit?

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Button-Controlled Loop

    make a static function in your class which will return the textedit pointer.

    btw, this question belongs to the newbie/C++ section

Similar Threads

  1. button background color when it is clicked
    By navi1084 in forum Qt Programming
    Replies: 4
    Last Post: 9th December 2008, 15:02
  2. button with backgr and icon using stylesheets
    By s_p_t10 in forum Qt Programming
    Replies: 0
    Last Post: 7th May 2008, 20:19
  3. question about button
    By narumi in forum Newbie
    Replies: 2
    Last Post: 21st January 2008, 05:44
  4. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55
  5. 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.