I want to update the background of my game after 10 seconds. I used singleShot
function of QTimer inside the connect function. It does work correctly for the first time but after the first call, update background function is being called after every 1 second (or so). I am new to Qt, please excuse my ignorance.
Here is the relevant code :
void Scene::setUpPillarTimer(QGraphicsPixmapItem* pixItem)
{
QTimer *backgroundTimer = new QTimer();
int durationOfPillar = 0;
pillarTimer = new QTimer(this);
connect(pillarTimer, &QTimer::timeout,this, [=]()mutable{
PillarItem *pillarItem = new PillarItem(durationOfPillar);
addItem(pillarItem);
backgroundTimer->singleShot(10000, this, [=](){
updateBackground(pixItem);
});
});
pillarTimer->start(800);
}
CodePudding user response:
So I removed the singleShot
function and inserted simple connect
function with a timeout 100000 ms.
void Scene::setUpPillarTimer(QGraphicsPixmapItem* pixItem)
{
QTimer *backgroundTimer = new QTimer(this);
int durationOfPillar = 0;
connect(backgroundTimer, &QTimer::timeout, this, [=](){
updateBackground(pixItem);
});
backgroundTimer->start(10000);
pillarTimer = new QTimer(this);
connect(pillarTimer, &QTimer::timeout,this, [=]()mutable{
PillarItem *pillarItem = new PillarItem(durationOfPillar);
addItem(pillarItem);
});
pillarTimer->start(800);
}
CodePudding user response:
It's hard to understand what you are looking for because your code is showing two different timers. Using a signle QTimer would be best but again I don't understand the original requirement for the 800 msec timer.
void Scene::setUpPillarTimer(QGraphicsPixmapItem* pixItem)
{
QTimer *backgroundTimer = new QTimer();
backgroundTimer->setInterval(10000); //msec
connect(backgroundTimer, &QTimer::timeout,this, [=]()mutable{
// Here this is executed every 10 seconds
PillarItem *pillarItem = new PillarItem(durationOfPillar);
addItem(pillarItem);
updateBackground(pixItem);
});
});
backgroundTimer->start();
}