Qt4 starting and stopping (pausing) - qt

Ok, so I'm having this problem tonight:
[...]
connect(startButton, SIGNAL(clicked()), this, SLOT(startCalculation()));
connect(stopButton, SIGNAL(clicked()), this, SLOT(stopCalculation()));
[...]
void MainWindow::startCalculation()
{
qDebug() << "hello";
this->startButton->setDisabled(true);
this->stopButton->setEnabled(true);
this->calcStatus = true;
this->calculate();
}
void MainWindow::stopCalculation()
{
this->startButton->setEnabled(true);
this->stopButton->setDisabled(true);
this->calcStatus = false;
}
void MainWindow::calculate()
{
qDebug() << "hello";
while(this->calcStatus)
{
}
}
[...]
I'm trying to make the calculate() procedure stoppable any time, but right after it is started I loose control and I can't press STOP. Of course in my future plans calculate() is going to "calculate" something real (e.g. heat transfer simulation).
Thanks for suggestions.
P.

You would need to look into threading. The calculation locks the ui.

Well, in "Introduction to Design Patterns in C++ with Qt4" they say that
"it is possible to avoid the use of
threads in favor of the Qt Event Loop
combined with QTimers"
but I've never tried it :)
Actually, I've just tried -
add:
QTimer *Timer;
in MainWindow class header and in MainWindow constructor add:
Timer = new QTimer(this);
then change calculate() from function to a signal and modify:
void MainWindow::startCalculation()
{
qDebug() << "hello";
this->startButton->setDisabled(true);
this->stopButton->setEnabled(true);
this->calcStatus = true;
connect(Timer, SIGNAL(timeout()), this, SLOT(calculate()));
Timer->start(0);
}
void MainWindow::stopCalculation()
{
this->startButton->setEnabled(true);
this->stopButton->setDisabled(true);
this->calcStatus = false;
Timer->stop();
Timer->disconnect(this,SLOT(calculate()));
}
This should work as long as you dont pass any arguments into calculate().

Related

QT-How to utilize QWidget with QThread?

I'm making some GUI through QT.
I almost complete my work but I have a hard time dealing with Qthread.
My goal is to measure the position of the motor (it moves) and display it on the Qtextbrowser while working another function in the main thread. When I wrote codes like below, people said I can't use QTextBrowser(Qwidget) directly in the thread, so I'm searching how to return location value to the main thread. Can you do me a favor?
MDCE is a class in another header and the codes I attach are some parts of my first code.
void MotorPort::StartThread(MDCE* com, QTextBrowser* browser)
{
thread1 = QThread::create(std::bind(&MotorPort::MeasureLocation,this,com,browser));
thread1 -> start();
}
void MotorPort::MeasureLocation(MDCE* com, QTextBrowser* browser)
{
double location;
while(1)
{
location = CurrentLocation(com); \\return current position value
browser->setText(QString::number(location));
if (QThread::currentThread()->isInterruptionRequested()) return ;
}
}
void MotorPort::stopMeasure()
{
thread1->requestInterruption();
if (!thread1->wait(3000))
{
thread1->terminate();
thread1->wait();
}
thread1 = nullptr;
}
You should use the Qt signal/slot mechanism for iter-thread notification such as this. Firstly change your MotorPort class definition to declare a signal location_changed...
class MotorPort: public QObject {
Q_OBJECT;
signals:
void location_changed(QString location);
...
}
Now, rather than MotorPort::MeasureLocation invoking QTextBrowser::setText directly it should emit the location_changed signal...
void MotorPort::MeasureLocation (MDCE *com, QTextBrowser *browser)
{
while (true) {
double location = CurrentLocation(com);
/*
* Emit signal to notify of location update.
*/
emit location_changed(QString::number(location));
if (QThread::currentThread()->isInterruptionRequested())
return ;
}
}
Finally, update MotorPort::StartThread to connect the signal to the browser's setText slot...
void MotorPort::StartThread (MDCE *com, QTextBrowser *browser)
{
connect(this, &MotorPort::location_changed, browser, &QTextBrowser::setText);
thread1 = QThread::create(std::bind(&MotorPort::MeasureLocation, this, com, browser));
thread1->start();
}

Connect Qtimer with a widget

I want to use a QTimer object to control a LED indictor status. A QLed class inherited QWidget is created to control the LED indicator. Here below its two major functions relevant:
void QLed::setLEDFlashing(bool value)
{
ledStatus = value; //Boolean value to accept a user-defined LED status
m_value = ledStatus; //m_value is used in painting LED (with QtSvgRenderer)
QTimer ledTimer;
ledTimer.setInterval(300);
if(!ledTimer.isActive())
{
ledTimer.start();
}
//Here is the connection between the timer and this (i.e., QLed*) object
connect(&ledTimer, SIGNAL(timeout()), this, SLOT(setLEDFlashingTimerHandler()));
}
//I want to use this function to make LED keep flashing
void QLed::setLEDFlashingTimerHandler()
{
//qDebug()<<"setLEDFlashingTimerHandler()";
if (ledStatus)
{
m_value = TRUE;
ledStatus = FALSE;
}
else
{
m_value = FALSE;
ledStatus =TRUE;
}
}
//This is to paint the LED widget
void QLed::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
//based on m_value, different svg file is loaded
if(m_value)
ledShapeAndColor.append(colors[m_onColor]);
else
ledShapeAndColor.append(colors[m_offColor]);
renderer->load(ledShapeAndColor);
renderer->render(&painter);
//qDebug()<<"paintEvent m_value="<<m_value;
}
In the mainwindow.ui, I add a QLabel object (named led) and promote it to QLed, and in the mainwindow.cpp:
ui->led->setLEDFlashing(TRUE);
The above codes cannot result in a flashing LED indicator. Actually, the connection between ledTimer and setLEDFlashingTimerHandler does NOT take effect for some reason, and m_value is not updated in paintEvent. Anyone can help debug my codes? Thanks!
Edits:
I have solved the connection issue by using QTimer *ledTimer in stead of QTimer ledTimer. But the painting still not works as expected, since m_value is not updated in that function or the function is only invoked for the very first time?
In your function QLed::setLEDFlashing, you create a local instance of QTimer which will be destroyed at the end of your function.
You should declare your QTimer as an attribute of your class or use a inner timer with QObject::startTimer

QT dont play sound if its already playing?

How can I make sure that a sound does not start to play if another one is already playing?
Im using the following code to play sounds:
void MainWindow::displayNotification(QString message)
{
// Play sound notification
QSound *sound = new QSound("://2_1.wav", this);
sound->play();
}
Any ideas?
Store a pointer to the playing QSound in your MainWindow, then check if it has finished with the isFinished function.
void MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
QString audiofile("://2_1.wav");
m_pSound = new QSound(audiofile, this);
if(!m_pSound)
{
qDebug() << "Failed to initialise sound file: " << audiofile;
}
}
void MainWindow::displayNotification(QString message)
{
if(!m_pSound) // check m_pSound is initialised
return;
// check if sound is playing
if(!m_pSound->isFinished)
return;
// Play sound notification
m_pSound->play();
}
Note that m_pSound is now declared as a member variable of MainWindow.
the class "QSound" has the method "isFinished()" for that reason.
if(m_pSound->isFinished())
//play
else
//wait

Read output of multiple write in QProcess one by one

Can anyone help me read the output of qprocess after write and loop until all task is done?
I have this code
wifi->write("scan\n");
wifi->closeWriteChannel();
wifi->waitForBytesWritten(100);
wifi->waitForReadyRead(100);
wifi->waitForFinished(100);
qDebug() << "read output" << wifi->readAllStandardOutput();
wifi->write("scan\n");
wifi->closeWriteChannel();
wifi->waitForBytesWritten(100);
wifi->waitForReadyRead(100);
wifi->waitForFinished(100);
qDebug() << "read output" << wifi->readAllStandardOutput();
the expected output must be
"OK"
"scan results"
but the ouput is
"OK"
""
thanks.
Your multiple waits are not useful for anything. All you care about is when the process finishes, so have a single waitForFinished call with a much longer timeout (those scans don't happen in ~100ms, a few seconds is a good minimum).
You should not be using the blocking waitForXxx methods. They trip up everyone and are a source of unending grief. Forget that they exist. Use process's signals to react to events as they happen.
Qt 5 + C++11
This is the way forward. This is why you should insist on using a modern development environment, if you can. It's less typing and easier to understand.
void MyObject::startWifi() {
auto process = new QProcess(this);
process->start("program", QStringList() << "argument");
connect(process, &QProcess::started, [process]{
process->write("scan\n");
process->closeWriteChannel();
});
connect(process, &QProcess::finished, [process]{
qDebug() << process->readAllStandardOutput();
process->deleteLater();
});
}
Qt 4
class MyObject : public QObject {
Q_OBJECT
QProcess m_wifi;
Q_SLOT void onStarted() {
m_wifi.write("scan\n");
m_wifi.closeWriteChannel();
}
Q_SLOT void onFinished() {
qDebug() << m_wifi.readAllStandardOutput();
}
public:
MyObject(QObject * parent = 0) : QObject(parent) {
connect(&m_wifi, SIGNAL(started()), SLOT(onStarted()));
connect(&m_wifi, SIGNAL(finished(int,QProcess::ExitStatus)),
SLOT(onFinished()));
}
Q_SLOT void start() {
m_wifi.start("program", QStringList() << "argument");
}
};
Then invoke the start method/slot on an instance of this object. That's all.

QTimer in a thread - events not processing?

I have an object that is derived from QThread and the class definition includes the Q_OBJECT macro. I created a timer within the thread so I can do some occasional checks while the thread is running; however, the timeout event is never occurring.
I've tried making the timer a singleshot as well, but no events are emitted.
Are events processed in a thread by default or do I need to do something else to have them processed?
Here's the code for how I set up the thread and timers:
void MyClass::run( void )
{
checkTimer_chA = new QTimer( this );
qDebug() << connect( checkTimer_chA, SIGNAL( timeout() ), this, SLOT( timerExpiry_chA() ) );
checkTimer_chA->start( 1000 );
// prevent multiple, simultaneous starts
if( !isRunning )
{
qDebug() << "Thread: MyClass::run";
isRunning = true;
while( isRunning )
{
getData();
processData();
yieldCurrentThread();
}
}
checkTimer_chA->stop();
delete checkTimer_chA;
}
void DAQ::timerExpiry_chA( void )
{
qDebug() << "timerExpiry_chA";
checkTimer_chA->stop();
}
If I add QApplication::processEvents(); right before the call to yieldCurrentThread(); the timer works as expected. However, this seems wrong to me.
Working with threads in Qt can sometimes be a bit of a hassle. This blog post was a real eye opener for me. In case we adopt the style proposed in the blog post to your problem we end up with a solution like below.
Consumer::Consumer():
checkTimer_(new QTimer(this))
{
QObject::connect(checkTimer_, SIGNAL(timeout()), this, SLOT(onTimerExpiration());
QObject::connect(this, SIGNAL(ready()), this, SLOT(consume());
}
bool Consumer::event(QEvent *e)
{
if (e->type() == QEvent::ThreadChange)
{
QTimer::singleShot(0, this, SLOT(start());
}
return QObject::event(e);
}
void Consumer::consume()
{
getData();
processData();
emit ready();
}
void Consumer::start()
{
checkTimer_->start(1000);
emit ready();
}
void Consumer::onTimerExpiration()
{
qDebug() << "timeout";
}
Then run it in a separate thread as follows:
...
Consumer *consumer = new Consumer();
...
QThread *thread = new QThread(this);
thread->start();
consumer->moveToThread(thread);
All child objects of Consumer will run in the context of the thread Consumer was moved to. It is possible to create a timeout signal for the Consumer class and connect it with an object that is not running in thread. Qt will ensure that the proper signal/slot types are applied to the connection once the object is moved to the thread.
I left out the whole isRunning part as I doubt you will still need it as long as you only create one Consumer.
Threads do not have their own event loops unless you explicitly create event loops in them. See http://doc.qt.io/qt-5/threads-qobject.html#per-thread-event-loop for details.
Perhaps you need an event loop running on the thread. What about changing the code to the following?
void MyClass::run( void )
{
checkTimer_chA = new QTimer( this );
qDebug() << connect( checkTimer_chA, SIGNAL( timeout() ), this, SLOT( timerExpiry_chA() ) );
checkTimer_chA->start( 1000 );
// prevent multiple, simultaneous starts
if( !isRunning )
{
qDebug() << "Thread: MyClass::run";
isRunning = true;
QTimer::singleShot(0, this, SLOT(process()));
exec();
}
checkTimer_chA->stop();
delete checkTimer_chA;
}
void MyClass::process()
{
if( isRunning )
{
getData();
processData();
yieldCurrentThread();
QTimer::singleShot(0, this, SLOT(process()));
}
else
QThread::exit();
}
void MyClass::timerExpiry_chA( void )
{
qDebug() << "timerExpiry_chA";
checkTimer_chA->stop();
}

Resources