MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
Qtの設定 - コマンドライン引数のソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
Qtの設定 - コマンドライン引数
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == Qt 5.2以降では、<code>[https://doc.qt.io/qt-5/qcommandlineparser.html QCommandLineParser]</code>クラスと<code>[https://doc.qt.io/qt-5/qcommandlineoption.html QCommandLineOption]</code>クラスが追加され、コマンドライン引数の扱いが簡潔になっている。<br> ここでは、これらのクラスの使用方法について記載する。<br> <br> また、デバッグの時にのみ使用するコマンドライン引数の設定方法も記載する。<br> <br><br> == サンプルコード == 以下のサンプルコードでは、<code>sl</code>コマンドのオプションに倣って、<code>-a</code>、<code>-l</code>、<code>-F</code>、<code>-h/–help</code>を解析している。<br> <source lang="c++"> #include <QtCore/QCoreApplication> #include <QtCore/QCommandLineParser> #include <QtCore/QDebug> int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); app.setApplicationName(QStringLiteral("sl - correct miss typing")); QCommandLineParser parser; parser.setApplicationDescription(QStringLiteral("sl is a highly developed animation program, which corrects your miss typing.")); parser.addHelpOption(); QCommandLineOption accidents(QStringLiteral("a"), QStringLiteral("It seems some accidents have happened. People give sad cries.")); parser.addOption(accidents); QCommandLineOption smaller(QStringLiteral("l"), QStringLiteral("Becomes smaller.")); parser.addOption(smaller); QCommandLineOption fly(QStringLiteral("F"), QStringLiteral("Flies.")); parser.addOption(fly); parser.process(app); if (parser.isSet(accidents)) { qDebug() << "Help!"; } if (parser.isSet(smaller)) { qDebug() << "smaller"; } if (parser.isSet(fly)) { qDebug() << "I can fly!"; } return 0; } </source> <br> # 実行 ./sl ./sl -a ./sl -l ./sl -a -l ./sl -al ./sl -h ./sl -S # 結果 Help! smaller Help! smaller Help! smaller Usage: ./sl [options] sl is a highly developed animation program, which corrects your miss typing. Options: -h, --help Displays this help. -a It seems some accidents have happened. People give sad cries. -l Becomes smaller. -F Flies. Unknown option 'S'. <br> <u>※備考</u><br> 様々なケースに対応できるような構造になっている。<br> 詳細は、<code>[https://doc.qt.io/qt-5/qcommandlineparser.html QCommandLineParser]</code>と<code>[https://doc.qt.io/qt-5/qcommandlineoption.html QCommandLineOption]</code>のドキュメントを参照すること。<br> また、他の使用方法については、[https://code.qt.io/cgit/qt/qtbase.git/tree/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp qtbase/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp]のテストケースを参照すること。<br> <br><br> == デバッグ時のコマンドライン引数の設定 == Qt Creatorを起動して、任意のQtプロジェクトを開く。<br> Qt Creatorの画面左にある[プロジェクト]アイコン - [Build & Run]セクション - [Run]項目を選択して、[実行時の設定]画面を表示する。<br> [実行時の設定]画面から、[実行]セクション - [コマンドライン引数]項目に、コマンドライン引数を記述する。<br> <br> コマンドライン引数の記述方法は、以下の通りである。<br> # 各引数は半角スペースで区切る hoge piyo fuga # 引数の内容に空白が入っている場合は、ダブルクォーテーション""で囲む "hoge piyo" fuga # 引数の内容にダブルクォーテーション""が入っている場合は、直前にエスケープシーケンスを記述する "hoge \" piyo" fuga <br><br> __FORCETOC__ [[カテゴリ:Qt]]
Qtの設定 - コマンドライン引数
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse