PDA

View Full Version : While statement inside the main loop



OnionRingOfDoom
8th February 2006, 05:04
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.


int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
/*for (int i=0; i < i + 1 ; i++)
{
window.label->setNum(i);
}*/

/*while (i)
{
window.label->setNum(i);
i++;
qApp->processEvents();
}*/
return app.exec();
}

Weaver
8th February 2006, 06:47
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.

Cesar
8th February 2006, 11:27
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

The second value is platform dependant
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().

OnionRingOfDoom
8th February 2006, 17:43
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:


while (i)
{
int amp = window.slider->ampSlider->value();
int freq = window.slider->freqSlider->value();
int sineValue = amp * ( sin(2 * M_PI * freq) * i);
i++;
}

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.

yop
8th February 2006, 19:17
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:


while (i)
{
int amp = window.slider->ampSlider->value();
int freq = window.slider->freqSlider->value();
int sineValue = amp * ( sin(2 * M_PI * freq) * i);
i++;
}
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 :)