
Originally Posted by
mahi6a1985
Dear then suggest me one example with 6 timers with different slots with different timers and they should run at a time.
It's not a problem to have six timers and execute them in time. It's a problem when you are trying to do more than your machine can handle.
Here is a simple example that shows that you can have six timers in an app:
#include <QtCore>
Q_OBJECT
public:
Dummy(){}
public slots:
void timeout() {
QTimer *timer
= qobject_cast<QTimer
*>
(sender
());
if(!timer) return;
qDebug
() <<
"Now is" <<
QDateTime::currentDateTime() <<
"timer id:" << timer
->property
("timerId").
toInt() <<
", interval is" << timer
->interval
() <<
"ms";
}
};
#include "main.moc"
int main(int argc, char **argv) {
Dummy d;
for(int i=0;i<6;++i) {
timer->setInterval(50+(qrand() % 200));
timer->setProperty("timerId", i);
QObject::connect(timer,
SIGNAL(timeout
()),
&d,
SLOT(timeout
()));
timer->start();
}
return app.exec();
}
#include <QtCore>
class Dummy : public QObject {
Q_OBJECT
public:
Dummy(){}
public slots:
void timeout() {
QTimer *timer = qobject_cast<QTimer*>(sender());
if(!timer) return;
qDebug() << "Now is" << QDateTime::currentDateTime() << "timer id:" << timer->property("timerId").toInt() << ", interval is" << timer->interval() << "ms";
}
};
#include "main.moc"
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
Dummy d;
for(int i=0;i<6;++i) {
QTimer *timer = new QTimer(&app);
timer->setInterval(50+(qrand() % 200));
timer->setProperty("timerId", i);
QObject::connect(timer, SIGNAL(timeout()), &d, SLOT(timeout()));
timer->start();
}
return app.exec();
}
To copy to clipboard, switch view to plain text mode
The main concept of my app is through tcp/ip client - server App. in which my app is client & which always sends 1 telegram every 100ms using "update_HmiScrLvl4".
and the telegram construction & values will produced by the other timers, so dat the constructed telegram can be sent to server using "update_HmiScrLvl4".
You don't need any threads for that nor any (not a single one!) timers. You just need to use QTcpSocket properly.
Bookmarks