我想在 10 秒后更新我的游戲背景。我singleShot在連接函式中使用了 QTimer 的函式。它第一次正常作業,但在第一次呼叫后,每隔 1 秒(左右)呼叫一次更新后臺函式。我是 Qt 的新手,請原諒我的無知。這是相關的代碼:
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);
}
uj5u.com熱心網友回復:
所以我洗掉了該singleShot函式并插入connect了超時 100000 毫秒的簡單函式。
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);
}
uj5u.com熱心網友回復:
很難理解您在尋找什么,因為您的代碼顯示了兩個不同的計時器。最好使用單 QTimer,但我還是不明白 800 毫秒計時器的原始要求。
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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/327425.html
上一篇:如何制作將顯示在所有QWizardPages上而不在每個頁面中復制它的小部件?
下一篇:在QML中模糊背景
