Hi,
I need to to manage serial port stream, and update my ui consequently.
Reading some post here in the forum I wrote the attached example zufolo.tar.gz, where the thread zotthread simulates serial port management.
here is the screenshot
screenshot.png
the application is very simple.
that are slots in dialog:
void Dialog::ovvaivai(void)
{
zotThread *zt;
zt = new zotThread;
connect(zt, SIGNAL(lancettasecondi(const QString&)), this, SLOT(cambiatestobottone(const QString&)));
connect(zt, SIGNAL(uscita()), this, SLOT(chiusura()));
zt->start();
}
void Dialog
::cambiatestobottone(const QString &s
) {
ui->pushButton->setText(s);
}
void Dialog::chiusura(void)
{
close();
}
void Dialog::ovvaivai(void)
{
zotThread *zt;
zt = new zotThread;
connect(zt, SIGNAL(lancettasecondi(const QString&)), this, SLOT(cambiatestobottone(const QString&)));
connect(zt, SIGNAL(uscita()), this, SLOT(chiusura()));
zt->start();
}
void Dialog::cambiatestobottone(const QString &s)
{
ui->pushButton->setText(s);
}
void Dialog::chiusura(void)
{
close();
}
To copy to clipboard, switch view to plain text mode
and this is the thread taht simulates serial management
void zotThread::run(void)
{
int i;
for(i=5;i>0;i--)
{
s.setNum(i);
emit lancettasecondi(s);
qt.setSingleShot(true);
qt.start(1000);
}
emit uscita();
}
void zotThread::run(void)
{
int i;
QString s;
QTimer qt;
for(i=5;i>0;i--)
{
s.setNum(i);
emit lancettasecondi(s);
qt.setSingleShot(true);
qt.start(1000);
while(qt.isActive()) QCoreApplication::processEvents();
}
emit uscita();
}
To copy to clipboard, switch view to plain text mode
when clicking the pushbutton ovvaivai(void) is called, so then thread starts. It perform a 5 seconds countdown before close the application.
I wonder if this is the right way to change the ui from a thread.
An also I wonder if is correct to call QCoreApplication::processEvents(); from inside trhead main loop. Without that call timer dosn't walk.
Now I want to read the spin value from thread. How can I perform that action? I don't think through signal/slot.
thanks
Bookmarks