PDA

View Full Version : Exit unending loop with Button



Roelof
17th September 2009, 16:17
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:


void Controller::standby()
{
powerDownButton->setEnabled(true);
standbyButton->setEnabled(false);

int val = 1;
while (val == 1)
{
read_data();

//here I nead something to exit the loop when a button is pressed;
//something that I can use to change val to another value and exit the loop
}


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

elmo
17th September 2009, 16:39
You have to create a new thread (with QThread) and place in it this unending loop.
See also this (http://doc.trolltech.com/4.5/threads.html).

Lykurg
17th September 2009, 18:13
or you use QCoreApplication::processEvents(). something like this:


void Controller::on_powerDownButton_clicked()
{
m_buttonPressed = true;
}

void Controller::standby()
{
powerDownButton->setEnabled(true);
standbyButton->setEnabled(false);
int val = 1;
m_buttonPressed = false; // member variable!
while (val == 1)
{
read_data();
QCoreApplication::processEvents();
if (m_buttonPressed)
{
//do your stuff here...
}
}
}


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

Roelof
18th September 2009, 14:20
Thanx Lykurg!!!!

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

I appreciate the help!