📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

7行目: 7行目:


== CUIソフトウェアの開発手順 ==
== CUIソフトウェアの開発手順 ==
例えば、以下のようなCUIソフトウェアのソースコードを記述したとする。<br>
例えば、以下に示すようなコンソールアプリケーションがあるとする。<br>
<br>
<br>
以下のソースコードにおいて、CUIソフトウェアは、永遠に終了しないプログラムになってしまう。<br>
このコンソールアプリケーションは、<code>QCoreApplication</code>クラスの<code>exec</code>メソッドによりアプリケーションは待機状態となり、<br>
なぜなら、<code>QCoreApplication</code>クラスの<code>exec</code>メソッドは、<code>exit</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>
この待機状態が不要の場合は、アプリケーションを終了する時に任意の地点で<code>exit</code>メソッドを呼ぶランナークラスを作成すればよい。<br>
<br>
<br>
例えば、以下のようなCUIRunnerクラスを定義する。<br>
例えば、以下に示すようなRunnerクラスを定義する。<br>
そして、CUIRunnerクラスのrunスロット内にメイン処理を記述して、処理を終了する時に<code>QCoreApplication</code>クラスの<code>exit</code>メソッドを呼び出せばよい。<br>
そして、Runnerクラスのrunスロット内にアプリケーションのメイン処理を記述して、処理を終了する時に<code>QCoreApplication</code>クラスの<code>exit</code>メソッドを呼び出す。<br>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  // CUIRunner.h
  // Runner.h
   
   
  #ifndef CUIRUNNER_H
  #ifndef RUNNER_H
  #define CUIRUNNER_H
  #define RUNNER_H
   
   
  #include <QObject>
  #include <QObject>
   
   
  class CUIRunner : public QObject
  class Runner : public QObject
  {
  {
     Q_OBJECT
     Q_OBJECT
   
   
  public slots:
  public slots:
     void run();  // runスロットメソッド内部でメイン処理を実行する
     void run();  // runスロットメソッドにアプリケーションのメイン処理を記述する
  };
  };
   
   
  #endif // CUIRUNNER_H
  #endif // RUNNER_H
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  // runメソッドの定義
  // Runner.cppファイル
  #include <QCoreApplication>
  #include <QCoreApplication>
  #include <CUIRunner.h>
  #include <Runner.h>
   
   
  void CUIRunner::run()
  void Runner::run()
  {
  {
     // メイン処理
     // アプリケーションのメイン処理
     // ...略
     // ...略
   
   
     QCoreApplication::exit(0);  // 閉じる(ループから抜ける)
     QCoreApplication::exit(0);  // アプリケーションを終了する
  }
  }
  </syntaxhighlight>
  </syntaxhighlight>
<br>
<br>
ここで重要なことは、<code>QTimer</code>クラスの<code>singleShot</code>メソッドのタイムアウト時間を0に設定していることである。<br>
重要なことは、<code>QTimer</code>クラスの<code>singleShot</code>メソッドのタイムアウト時間を<code>0</code>に設定することである。<br>
このように記述することにより、ウインドウシステムの全てのイベント(描画処理等)が処理された後に、runメソッドで定義した処理が実行される。<br>
これは、アプリケーションの全てのイベント (描画処理等) が処理された後に、runメソッドで定義した処理が実行される。<br>
  <syntaxhighlight lang="c++">
  <syntaxhighlight lang="c++">
  #include <QCoreApplication>
  #include <QCoreApplication>
  #include <QTimer>
  #include <QTimer>
  #include "CUIRunner.h"
  #include "Runner.h"
   
   
  int main(int argc, char *argv[])
  int main(int argc, char *argv[])
75行目: 76行目:
   
   
     // ランナー開始
     // ランナー開始
     CUIRunner runner;
     Runner runner;
     QTimer::singleShot(0, &runner, SLOT(run()));
     QTimer::singleShot(0, &runner, SLOT(run()));