Results 1 to 5 of 5

Thread: While statement inside the main loop

  1. #1

    Default While statement inside the main loop

    So I'm trying to have a while loop executing durring the main loop of my program. IE, the while loop goes in the background, and it lets me use the program like normal. I can't seem to get this right, and what I tried was putting it in the main loop, right in between MainWindow.show(). and return app.exec();. However, this just made the while loop iterate once. I tried it the same way with a for loop, and the for loop goes until it stops, and lets me use the program durring the loop, but it doesn't let me use any of the current variable values, only the values the variables had when the program started.

    Here's my int main() function. The two loop statements I tried are in the block comments.
    The i used in the while statement is initialised globally at the beginning of the program, right after the include statements.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. MainWindow window;
    5. window.show();
    6. /*for (int i=0; i < i + 1 ; i++)
    7.   {
    8.   window.label->setNum(i);
    9.   }*/
    10.  
    11. /*while (i)
    12.   {
    13.   window.label->setNum(i);
    14.   i++;
    15.   qApp->processEvents();
    16.   }*/
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    New Zealand
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: While statement inside the main loop

    The exec() method in QApplication starts the event handler, so your MainWindow widget isn't really active until that line is hit.

    To do what it looks like you're trying to achieve, you could either use a QTimer to run your setNum function periodically. Or if you want to get complicated you could use a QThread, but that's likely more hassle that it's worth.

  3. #3
    Join Date
    Feb 2006
    Location
    Kirovohrad, Ukraine
    Posts
    72
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: While statement inside the main loop

    Both loops look strange. As far as I understand you'd like the label to show the values staring from 1 up to 2 ^ sizeof(int) - 1
    1. The second value is platform dependant
    2. Even if you move your loop to other place, ie to the constructor of MainWindow, you won't notice label's changes. You should make sure that label's paintEvent occurs each time you do label->setNum().

  4. #4

    Default Re: While statement inside the main loop

    Well, I'm not really trying to do anything with the label, it was just there for testing purposes. I figured that if I could get that label to work right, then I could get whatever else I wanted to put in the while statement to work. In reality, I'll be using this code:
    Qt Code:
    1. while (i)
    2. {
    3. int amp = window.slider->ampSlider->value();
    4. int freq = window.slider->freqSlider->value();
    5. int sineValue = amp * ( sin(2 * M_PI * freq) * i);
    6. i++;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Or at least something similar to it. In other words, I want it to be giving me the y axis of a sine wave while the program runs, so that it lets me change the amplitude and the frequency using the slider, and updates the sine wave accordingly. The i is there so the sine wave moves along the x axis, resulting in a smooth up and down motion.

  5. #5
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: While statement inside the main loop

    Quote Originally Posted by OnionRingOfDoom
    Well, I'm not really trying to do anything with the label, it was just there for testing purposes. I figured that if I could get that label to work right, then I could get whatever else I wanted to put in the while statement to work. In reality, I'll be using this code:
    Qt Code:
    1. while (i)
    2. {
    3. int amp = window.slider->ampSlider->value();
    4. int freq = window.slider->freqSlider->value();
    5. int sineValue = amp * ( sin(2 * M_PI * freq) * i);
    6. i++;
    7. }
    To copy to clipboard, switch view to plain text mode 
    I 'd suggest a timer and updating the graph each time it times out. Don't use the while loop especially since it's not necessary as the event loop will be blocked and you'll have to processEvents() if you use it.

    Edit: I only show the last post and Weaver has suggested the same thing

Similar Threads

  1. HAL & DBusConnection & Qt main loop integration
    By juannm in forum Qt Programming
    Replies: 3
    Last Post: 15th January 2009, 08:06
  2. Replies: 4
    Last Post: 26th June 2008, 18:41
  3. Main Thread Event loop
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 12:10
  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

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.