我是 Qt 編程的新手,我想讓計時器成為空閑計時器。問題是僅設定 timer->start(0) 是否會使計時器成為空閑計時器?我怎么知道它是一個空閑計時器。
uj5u.com熱心網友回復:
我想重復
超時為 0 的應用程式永久占用(多于)一個核心,并導致高額的貨幣消耗。
在這里我們開始:QTimer間隔為0的a:
#include <QtWidgets>
// main application
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
QWidget qWinMain;
qWinMain.setWindowTitle("QTimer - timeout 0");
QVBoxLayout qVBox;
QLabel qLblText("Attention!\nThis may cause ventilation noise.");
qVBox.addWidget(&qLblText);
QLabel qLblI;
qVBox.addWidget(&qLblI);
qWinMain.setLayout(&qVBox);
qWinMain.show();
QTimer qTimer;
qTimer.setInterval(0);
ushort i = 0;
// install signal handlers
QObject::connect(&qTimer, &QTimer::timeout,
[&]() {
i;
if (i) qLblI.setText(QString("i: %1").arg(i));
else app.quit();
});
// runtime loop
qTimer.start();
return app.exec();
}
輸出:

間隔 0 使計時器立即到期。這意味著超時事件被附加到事件佇列中。
因此,我希望佇列永久填充超時和繪制事件(用于更新qLblI),直到應用程式退出。
I once used such an idle event in the past (before I started to use Qt). I intended to combine the UI event loop with polling "events" from a different source (and lacking any idea about a better alternative). Thereby, the polling function provided itself a time-out option. Thus, the call of the poll function suspended itself the process until either an event came in or the timeout was reached. The most intricate thing was to achieve somehow a load balancing as I tried to distribute the available time equally between the UI and the processing of the other event source. (This was important for the situation where UI events were approaching in high frequency concurrently with the other event source.)
However, I doubt that such a fiddling would be still necessary in Qt. For such a concurrency, there are better options e.g. to run a blocking function in a separate thread.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434931.html
上一篇:JFrame中的間距問題
