📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 372行目: | 372行目: | ||
== タイマを使用したスリープ == | == タイマを使用したスリープ == | ||
以下の例では、<code>QEventLoop</code>クラスを使用したスリープ処理である。<br> | 以下の例では、<code>QEventLoop</code>クラスを使用したスリープ処理である。<br> | ||
これにより、CPUに負荷を掛けずにイベントシステムを使用してタイマを終了することができる。<br> | |||
<br> | |||
ボタン押下時にDelay関数を実行して、3秒間の待機を実演している。<br> | |||
待機中もUIは応答可能であり、他のイベントを処理することができる。<br> | |||
<br> | |||
具体的な動作を以下に示す。<br> | |||
* EventLoopクラスの使用 | |||
*: <code>QEventLoop</code>クラスは、イベントの処理を一時的に独立したループで行うことを可能にする。 | |||
*: これにより、アプリケーションのメインイベントループをブロックせずに待機することができる。 | |||
* QTimerクラスとの組み合わせ | |||
*: <code>QTimer</code>クラスは、指定された時間後に<code>timeout</code>シグナルを発行する。 | |||
*: このシグナルを<code>QEventLoop::quit</code>スロットに接続することにより、タイマが満了した時にループを終了する。 | |||
* 効率的な待機 | |||
*: この方法では、ビジーウェイトを使用せずに待機するため、CPUリソースを節約できる。 | |||
*: アプリケーションは他のイベントに応答可能な状態を維持する。 | |||
* 非同期処理 | |||
*: onDelayButtonClickedスロットでは、<code>QTimer::singleShot</code>メソッドを使用して、performDelayを非同期で呼び出している。 | |||
*: これにより、UIのフリーズを防いで、ユーザエクスペリエンスを向上させている。 | |||
* 状態表示 | |||
*: QLabelを使用して、遅延の開始と終了を表示している。 | |||
*: これにより、ユーザに処理の進行状況を視覚的に伝えることができる。 | |||
<br> | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// MainWindow.hファイル | |||
#include <QMainWindow> | |||
#include <QEventLoop> | |||
#include <QPushButton> | |||
#include <QVBoxLayout> | |||
#include <QLabel> | |||
#include <QTimer> | #include <QTimer> | ||
void MainWindow::Delay(int | class MainWindow : public QMainWindow | ||
{ | |||
Q_OBJECT | |||
private: | |||
QLabel *m_statusLabel; | |||
public: | |||
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) | |||
{ | |||
QWidget *centralWidget = new QWidget(this); | |||
setCentralWidget(centralWidget); | |||
QVBoxLayout *layout = new QVBoxLayout(centralWidget); | |||
QPushButton *delayButton = new QPushButton("Start 3 Second Delay", this); | |||
layout->addWidget(delayButton); | |||
m_statusLabel = new QLabel("Ready", this); | |||
layout->addWidget(m_statusLabel); | |||
connect(delayButton, &QPushButton::clicked, this, &MainWindow::onDelayButtonClicked); | |||
} | |||
void Delay(int ms) | |||
{ | |||
QEventLoop loop; | |||
QTimer timer; | |||
timer.setSingleShot(true); | |||
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); | |||
timer.start(ms); | |||
loop.exec(); | |||
} | |||
private slots: | |||
void onDelayButtonClicked() | |||
{ | |||
m_statusLabel->setText("Starting delay..."); | |||
QTimer::singleShot(0, this, &MainWindow::performDelay); | |||
} | |||
void performDelay() | |||
{ | |||
Delay(3000); // 3秒の遅延 | |||
m_statusLabel->setText("Delay finished!"); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// main.cppファイル | |||
#include "MainWindow.h" | |||
int main(int argc, char *argv[]) | |||
{ | { | ||
QApplication app(argc, argv); | |||
MainWindow window; | |||
window.show(); | |||
return app.exec(); | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||