Exit unending loop with Button
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:
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!
Re: Exit unending loop with Button
You have to create a new thread (with QThread) and place in it this unending loop.
See also this.
Re: Exit unending loop with Button
or you use QCoreApplication::processEvents(). something like this:
Code:
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();
if (m_buttonPressed)
{
//do your stuff here...
}
}
}
depends on your task what is better. This or a separate thread.
Re: Exit unending loop with Button
Thanx Lykurg!!!!
I was afraid I had to do mutlithreading, but this is the solution I was looking for.
I appreciate the help!