📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
細 文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag… |
|||
| (同じ利用者による、間の7版が非表示) | |||
| 1行目: | 1行目: | ||
== 概要 == | == 概要 == | ||
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> | 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> | ||
また、<code>QCoreApplication</code>クラスの<code>arguments</code>メソッドを使用することでも、コマンドライン引数を扱うことができる。<br> | |||
<br> | |||
ここでは、これらのクラスの使用方法について記載する。<br> | ここでは、これらのクラスの使用方法について記載する。<br> | ||
併せて、デバッグ時のみ使用するコマンドライン引数の設定方法も記載する。<br> | |||
<br><br> | <br><br> | ||
== | == QCommandLineParserクラスの使用 == | ||
以下の例では、<code>QCommandLineParser</code>クラスを継承したサブクラスを定義して、指定されたオプションを処理している。<br> | |||
< | <br> | ||
#include < | <code>QCommandLineParser</code>クラスのデフォルトの動作により、例えば、--hオプションが-hオプションと同様に処理されてしまう。<br> | ||
#include < | この問題を解決するために、<code>QCommandLineParser</code>クラスの<code>process</code>メソッドをオーバーライドして、カスタムの解析ロジックを実装している。<br> | ||
#include < | <br> | ||
正規表現を使用して、有効なオプション (-v, -h, -c, --version, --help, --config) のみを受け付けるようにする。<br> | |||
上記に存在しないオプションが指定された場合は、"不明なオプション"として処理する。<br> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// CustomCommandLineParser.hファイル | |||
#include <QCoreApplication> | |||
#include <QCommandLineParser> | |||
#include <QRegularExpression> | |||
#include <QDebug> | |||
class CustomCommandLineParser : public QCommandLineParser | |||
{ | |||
public: | |||
void process(const QStringList &arguments) | |||
{ | |||
QStringList filteredArgs; | |||
static QRegularExpression validOptionRegex("^(-v|-h|-c|--version|--help|--config)(?:=.*)?$"); | |||
for (const QString &arg : arguments) { | |||
if (validOptionRegex.match(arg).hasMatch()) { | |||
filteredArgs << arg; | |||
} | |||
else if (arg.startsWith("-")) { | |||
qDebug() << "不明なオプション:" << arg; | |||
} | |||
else { | |||
filteredArgs << arg; | |||
} | |||
} | |||
QCommandLineParser::process(filteredArgs); | |||
} | |||
}; | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// main.cppファイル | |||
#include "CustomCommandLineParser.h" | |||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
QCoreApplication | QCoreApplication a(argc, argv); | ||
QCoreApplication::setApplicationName("<任意のアプリケーション名>"); | |||
QCoreApplication::setApplicationVersion("1.0"); | |||
CustomCommandLineParser parser; | |||
parser.setApplicationDescription | parser.setApplicationDescription("Command line options example"); | ||
QCommandLineOption | QCommandLineOption versionOption(QStringList() << "v" << "version", "Displays version information."); | ||
parser.addOption( | parser.addOption(versionOption); | ||
QCommandLineOption | QCommandLineOption helpOption(QStringList() << "h" << "help", "Displays help information."); | ||
parser. | parser.addOption(helpOption); | ||
QCommandLineOption configOption(QStringList() << "c" << "config", "Display the configuration string.", "config"); | |||
parser.addOption(configOption); | |||
parser.process(QCoreApplication::arguments()); | |||
if (parser.isSet(helpOption)) { | |||
parser.showHelp(); | |||
return 0; | |||
} | |||
if (parser.isSet(versionOption)) { | |||
qDebug() << QCoreApplication::applicationName() << QCoreApplication::applicationVersion(); | |||
if (parser.isSet( | return 0; | ||
qDebug() << | |||
} | } | ||
if (parser.isSet( | if (parser.isSet(configOption)) { | ||
QString configString = parser.value(configOption); | |||
qDebug() << " | qDebug() << "Configuration: " << configString; | ||
return 0; | |||
} | } | ||
if (parser. | if (parser.positionalArguments().isEmpty() && !parser.optionNames().isEmpty()) { | ||
qDebug() << "有効なオプションはありません。使用法については、-h または --help を使用してください。"; | |||
qDebug() << " | |||
} | } | ||
return 0; | return 0; | ||
} | } | ||
</ | </syntaxhighlight> | ||
<br> | <br> | ||
# 実行 | # 実行 | ||
<実行ファイル名> -h または <実行ファイル名> --help | |||
ヘルプ情報を表示する | |||
<実行ファイル名> -v または <実行ファイル名> --version | |||
バージョン情報を表示する | |||
<実行ファイル名> -c "<任意の文字列>" または <実行ファイル名> --config "<任意の文字列>" | |||
指定された設定文字列を表示する | |||
<実行ファイル名> -x | |||
"不明なオプション: x" と表示する | |||
<br> | |||
<u>※注意</u><br> | |||
<u><code>QCommandLineParser</code>クラスは、様々なケースに対応できるような構造になっている。</u><br> | |||
<br> | |||
<u><code>QCommandLineParser</code>クラスの詳細は、Qt公式ドキュメントを参照すること。</u><br> | |||
* QCommandLineParserクラス | |||
*: Qt 6 : https://doc.qt.io/qt-6/qcommandlineparser.html | |||
*: Qt 5 : https://doc.qt.io/qt-5/qcommandlineparser.html | |||
* QCommandLineOptionクラス | |||
*: https://doc.qt.io/qt-6/qcommandlineoption.html | |||
*: https://doc.qt.io/qt-5/qcommandlineoption.html | |||
<br> | <br> | ||
また、他の使用方法については、<code>QCommandLineParser</code>クラスのテストケースを参照すること。<br> | |||
* https://code.qt.io/cgit/qt/qtbase.git/tree/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp | |||
<br><br> | <br><br> | ||
== | == QCoreApplicationクラスを使用する方法 == | ||
上記とは別の方法として、<code>QCoreApplication::arguments()</code>を使用することもできる。<br> | 上記とは別の方法として、<code>QCoreApplication::arguments()</code>を使用することもできる。<br> | ||
<code>QCoreApplication::arguments()</code>の型は<code>QStringList</code>であるため、簡単に取得および加工することができる。<br> | <code>QCoreApplication::arguments()</code>の型は<code>QStringList</code>であるため、簡単に取得および加工することができる。<br> | ||
<br> | <br> | ||
以下の3つの例では、全てのコマンドライン引数を<code>QStringList</code>クラスで取得して表示している。<br> | |||
コマンドライン引数の1つ目の要素を削除している理由は、1つ目の引数にはソフトウェアのパスが渡されるからである。<br> | コマンドライン引数の1つ目の要素を削除している理由は、1つ目の引数にはソフトウェアのパスが渡されるからである。<br> | ||
<syntaxhighlight lang="c++"> | <syntaxhighlight lang="c++"> | ||
// 方法 1 | |||
QStringList argv = QCoreApplication::arguments(); | QStringList argv = QCoreApplication::arguments(); | ||
| 106行目: | 145行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br> | <br> | ||
<syntaxhighlight lang="c++"> | |||
// 方法 2 | |||
QStringList argv = QCoreApplication::arguments(); | |||
for(int i = 1; i < arguments.count(); i++) | |||
{ | |||
qDebug() << "argument = " << arguments.at(i); | |||
} | |||
</syntaxhighlight> | |||
<br> | |||
<syntaxhighlight lang="c++"> | |||
// 方法 3 | |||
QStringList argv = QCoreApplication::arguments(); | |||
argv.removeAt(0); | |||
foreach(QString const &arg, arguments) | |||
{ | |||
qDebug() << arg; | |||
} | |||
</syntaxhighlight> | |||
<br><br> | <br><br> | ||
== | == デバッグ時におけるコマンドライン引数の設定 == | ||
Qt Creatorを起動して、任意のQtプロジェクトを開く。 | # Qt Creatorを起動して、任意のQtプロジェクトを開く。 | ||
Qt | # Qt Creatorのメイン画面左にある[プロジェクト]アイコン - [Build & Run]セクション - [Run]項目を選択して、[実行時の設定]画面を表示する。 | ||
[実行時の設定]画面から、[実行]セクション - [コマンドライン引数]項目に、コマンドライン引数を記述する。 | # [実行時の設定]画面から、[実行]セクション - [コマンドライン引数:]項目に、コマンドライン引数を記述する。 | ||
<br> | <br> | ||
コマンドライン引数の記述方法は、以下の通りである。<br> | コマンドライン引数の記述方法は、以下の通りである。<br> | ||
| 124行目: | 182行目: | ||
"hoge \" piyo" fuga | "hoge \" piyo" fuga | ||
<br><br> | <br><br> | ||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:Qt]] | [[カテゴリ:Qt]] | ||