PDA

View Full Version : Button-Controlled Loop



freekill
16th May 2009, 03:44
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.:(

Lykurg
16th May 2009, 09:11
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 (http://doc.trolltech.com/qq/qq27-responsive-guis.html).

freekill
4th June 2009, 07:41
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:(



#include <QtGui>
#include "looper.h"

bool stop;
void mainLoop();

basicLoop::basicLoop(QWidget *parent)
{
setupUi(this); // this sets up GUI

connect( startButton, SIGNAL( clicked() ), this, SLOT( startLoop() ) );
connect( stopButton, SIGNAL( clicked() ), this, SLOT( stopLoop() ) );
}

void basicLoop::startLoop()
{
&stop = false;
mainLoop();
}

void basicLoop::stopLoop()
{
&stop = true;
}

void mainLoop()
{
int i=0;
//check for stop button press
QCoreApplication::processEvents();
if (stopButton->isChecked) stopLoop();
while(!&stop)
{
textEdit->append(QString::number(i));
i++;
}
}


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..

nish
4th June 2009, 07:50
this is how its done.


void mainLoop()
{
int i=0;
//check for stop button press
//QCoreApplication::processEvents();//Put this inside loop
//if (stopButton->isChecked) stopLoop();//no need for this
while(!&stop)
{
QCoreApplication::processEvents();
textEdit->append(QString::number(i));
i++;
}
}

freekill
4th June 2009, 09:17
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?

nish
4th June 2009, 10:09
make a static function in your class which will return the textedit pointer.

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