自分用メモ: Qtコマンドラインアプリのmain.cppとNotes C APIの初期化

典型的なQtコマンドラインアプリのmain.cppの書き方。

#include <QCoreApplication>

int main(int argc, char *argv[])
{
  QCoreApplication app(argc, argv);
  // 処理
  return 0;
}

Qtのイベントループをコマンドラインでも使いたい場合、QTimer::singleShotが使える。 qApp->exit(0)を使わずにapp.exec()を呼び出すと、イベントループを抜け出せなくなるので注意する。

#include <QCoreApplication>
#include <QObject>
#include <QTimer>

class DoSomething : public QObject
{
  Q_OBJECT
public: slots:
  void run() {
    // 処理
    qApp->exit(0);
  }
};

int main(int argc, char *argv[])
{
  QCoreApplication app(argc, argv);
  DoSomething doSomething;
  QTimer::singleShot(0, &doSomething, SLOT(run()));
  return app.exec();
}

Qt5とC++11を使うと、ラムダ式で呼び出せるようになり、記述が楽になる。

#include <QCoreApplication>
#include <QTimer>

int main(int argc, char *argv[])
{
  QCoreApplication app(argc, argv);
  QTimer::singleShot(0, [&]() {
    // 処理
    app.exit(0);
  });
  return app.exec();
}

これに、Notes C APIの初期化を組み合わせると、こんな感じになる。

#include <QCoreApplication>
#include <QTimer>

#ifdef NT
#pragma pack(push, 1)
#endif

#include <global.h>

#ifdef NT
#pragma pack(pop, 1)
#endif

int main(int argc, char *argv[])
{
  STATUS status = NotesInitExtended(argc, argv);
  if (ERR(status) != NOERROR) return 1;

  QCoreApplication app(argc, argv);
  QTimer::singleShot(0, [&]() {
    // 処理
    app.exit(0);
  });
  int result = app.exec();
  NotesTerm();
  return result;
}