我正在使用 UI 在 QT 上制作應用程式。另外,我有一個功能。我想顯示一個函式的運行時間。我也想暫停秒表。關于如何在我的應用程式中正確嵌入秒表的任何想法?這是我的代碼:
void SomeFunc()
{
while (1)
{
// Start Timer
// some code
// Stop Timer
// Start Timer2
// some code
// Stop Timer2
}
}
on_push_button()
{
auto futureWatcher = new QFutureWatcher<void>(this);
QObject::connect(futureWatcher, &QFutureWatcher<void>::finished, futureWatcher, &QFutureWatcher<void>::deleteLater);
auto future = QtConcurrent::run( [=]{ PackBytes( file_path, file_name, isProgressBar); });
futureWatcher->setFuture(future);
}
uj5u.com熱心網友回復:
用于QElapsedTimer測量自開始計算以來的持續時間。從您之前關于非常相關主題的問題來看,您確實有一個MainWindow包含該on_push_button函式的類。在該類中,宣告QElapsedTimer成員;然后在您的計算開始時啟動它。
使用 aQTimer更新用于顯示當前持續時間的 GUI 元素(只需要每秒運行一次左右)。
示例代碼:
在你的頭檔案中:
#include <QElapsedTimer>
#include <QTimer>
//...
class MainWindow
{
// ... other stuff you have
private:
QTimer m_stopwatchUpdate;
QElapsedTimer m_stopwatchElapsed;
};
在您的 cpp 檔案中:
void MainWindow::on_push_button()
{
// ideally do this once only in the constructor of MainWindow:
connect(&m_stopwatchUpdate, &QTimer::timeout, this, [=]{
//.. update GUI elements here...
});
auto futureWatcher = new QFutureWatcher<void>(this);
connect(futureWatcher, &QFutureWatcher<void>::finished, futureWatcher, &QFutureWatcher<void>::deleteLater);
auto future = QtConcurrent::run( [=]{
m_stopwatchElapsed.start();
PackBytes(file_path, file_name, isProgressBar);
});
m_stopwatchUpdate.start(1000);
futureWatcher->setFuture(future);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/463654.html
