📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
| 7行目: | 7行目: | ||
== CUIソフトウェアの開発手順 == | == CUIソフトウェアの開発手順 == | ||
例えば、以下に示すようなコンソールアプリケーションがあるとする。<br> | |||
<br> | <br> | ||
このコンソールアプリケーションは、<code>QCoreApplication</code>クラスの<code>exec</code>メソッドによりアプリケーションは待機状態となり、<br> | |||
<code>exit</code>メソッドを呼ぶまで待機を続ける。<br> | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
int main(int argc, char * argv[]) | int main(int argc, char * argv[]) | ||
| 17行目: | 17行目: | ||
int count = 1000; | int count = 1000; | ||
while(--count) | while(--count) { | ||
printf("Count = %d\n", count); | printf("Count = %d\n", count); | ||
} | } | ||
| 26行目: | 25行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
この待機状態が不要の場合は、アプリケーションを終了する時に任意の地点で<code>exit</code>メソッドを呼ぶランナークラスを作成すればよい。<br> | |||
<br> | <br> | ||
例えば、以下に示すようなRunnerクラスを定義する。<br> | |||
そして、Runnerクラスのrunスロット内にアプリケーションのメイン処理を記述して、処理を終了する時に<code>QCoreApplication</code>クラスの<code>exit</code>メソッドを呼び出す。<br> | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// | // Runner.h | ||
#ifndef | #ifndef RUNNER_H | ||
#define | #define RUNNER_H | ||
#include <QObject> | #include <QObject> | ||
class | class Runner : public QObject | ||
{ | { | ||
Q_OBJECT | Q_OBJECT | ||
public slots: | public slots: | ||
void run(); // | void run(); // runスロットメソッドにアプリケーションのメイン処理を記述する | ||
}; | }; | ||
#endif // | #endif // RUNNER_H | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// | // Runner.cppファイル | ||
#include <QCoreApplication> | #include <QCoreApplication> | ||
#include < | #include <Runner.h> | ||
void | void Runner::run() | ||
{ | { | ||
// | // アプリケーションのメイン処理 | ||
// ...略 | // ...略 | ||
QCoreApplication::exit(0); // | QCoreApplication::exit(0); // アプリケーションを終了する | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
重要なことは、<code>QTimer</code>クラスの<code>singleShot</code>メソッドのタイムアウト時間を<code>0</code>に設定することである。<br> | |||
これは、アプリケーションの全てのイベント (描画処理等) が処理された後に、runメソッドで定義した処理が実行される。<br> | |||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
#include <QCoreApplication> | #include <QCoreApplication> | ||
#include <QTimer> | #include <QTimer> | ||
#include " | #include "Runner.h" | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
| 75行目: | 76行目: | ||
// ランナー開始 | // ランナー開始 | ||
Runner runner; | |||
QTimer::singleShot(0, &runner, SLOT(run())); | QTimer::singleShot(0, &runner, SLOT(run())); | ||