このページでは

QHttpServerRouter Class

ViewHandler にパスをバインドする機能を提供します。詳細...

ヘッダー: #include <QHttpServerRouter>
CMake: find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
qmake: QT += httpserver
以下のように: Qt 6.4 以降

パブリック関数

QHttpServerRouter(QAbstractHttpServer *server)
~QHttpServerRouter()
bool addConverter(QAnyStringView regexp)
void addConverter(QMetaType metaType, QAnyStringView regexp)
QHttpServerRouterRule *addRule(std::unique_ptr<QHttpServerRouterRule> rule)
void clearConverters()
QHash<QMetaType, QString> converters() &&
const QHash<QMetaType, QString> &converters() const &
bool handleRequest(const QHttpServerRequest &request, QHttpServerResponder &responder) const
void removeConverter(QMetaType metaType)

詳細な説明

QHttpServerRouterは、HTTPリクエストを適切なハンドラにルーティングするためのルールベースのシステムです。リクエストパスとそれに対応するハンドラを定義するQHttpServerRouterRule インスタンスを追加することができます。

ルーティング内の可変部分は、リクエストパス内のプレースホルダー("<arg>" )で指定できますが、末尾での指定は必須ではありません。ハンドラは、一致した値をQRegularExpressionMatch として受け取ります。引数は、converter が利用可能な任意の型にすることができます。ハンドラの作成は、QHttpServerRouterRule::bindCaptured() を使用することで簡略化できます。

注:QHttpServerRouterインスタンスは 、そのルールによって変更されてはなりません。

注:これは 、HTTP サーバー向けの低レベルのルーティング API です。

最小限の例:

autopageView= [](constquint64 page) {
    qDebug() << "page" << page;
};
usingViewHandler=decltype(pageView);

QHttpServerRouter router;

// リクエスト "/page/<number>" に対してコールバック pageView を登録
// 例: "/page/10", "/page/15"
router.addRule<ViewHandler>(
    newQHttpServerRouterRule("/page/", [=](QRegularExpressionMatch&match,
                                             constQHttpServerRequest&,
                                             QHttpServerResponder&&) {
    autoboundView=QHttpServerRouterRule::bindCaptured(pageView,match);

    // pageView を呼び出す
    boundView();
}));

メンバ関数のドキュメント

QHttpServerRouter::QHttpServerRouter(QAbstractHttpServer *server)

デフォルトのコンバータを持つ QHttpServerRouter オブジェクトを作成します。

convertersも参照してください 。

[noexcept] QHttpServerRouter::~QHttpServerRouter()

QHttpServerRouter を破棄します。

template <typename Type> bool QHttpServerRouter::addConverter(QAnyStringView regexp)

regexp で解析可能なType用の新しいコンバータを追加し、解析に成功した場合はtrue を返し、失敗した場合はfalse を返します。成功した場合、登録された型はQHttpServerRouterRule のハンドラで引数として使用できます。正規表現は、ルールのパスパターンを解析するために使用されます。

Type 型のコンバータがすでに存在する場合、そのコンバータの正規表現はregexp に置き換えられます。

カスタムコンバータは、QMetaType システムを通じて、利用可能な型変換を拡張することができます。

QString コンストラクタを持つクラスを定義します:

struct CustomArg {
    int data = 10;

    CustomArg() {} ;
    CustomArg(const QString &urlArg) : data(urlArg.toInt()) {}
};

HTTP server でカスタム型を使用するには、この関数を使用して登録し、新しい型を使用して `route ` ハンドラを定義します:

server.router()->addConverter<CustomArg>(u"[+-]?\\d+");
server.route("/customTest/<arg>", [] (const CustomArg custom) { return QString::number(custom.data); });

「 converters 」および「clearConverters 」も参照してください 。

void QHttpServerRouter::addConverter(QMetaType metaType, QAnyStringView regexp)

`metaType ` 用の新しいコンバータを追加します。これは `regexp` で解析可能です。metaType 用のコンバータを用意することで、QHttpServerRouterRule のパスパターン内でこの型を使用できるようになります。この正規表現は、パスパターンから `metaType ` 型のパラメータを解析するために使用されます。

metaType 型のコンバータがすでに存在する場合、そのコンバータの正規表現はregexp に置き換えられます。

「 converters 」および「clearConverters 」も参照してください 。

template <typename ViewHandler, typename ViewTraits = QHttpServerRouterViewTraits<ViewHandler>> QHttpServerRouterRule *QHttpServerRouter::addRule(std::unique_ptr<QHttpServerRouterRule> rule)

ルーターに新しいrule を追加します。

成功した場合は新しいルールへのポインタを返し、そうでない場合はnullptr を返します。

addRule 内部では、ViewHandler の引数を判定し、それらのQMetaType::Type ID のリストを生成します。次に、パスを解析し、各"<arg>" を、リストにあるそのタイプに対応する正規表現に置き換えます。

渡されたrule は、QHttpServerRouter インスタンスを変更してはなりません。

QHttpServerRouter router;

using ViewHandler = decltype([] (const QString &page, const quint32 num) { });

auto rule = std::make_unique<QHttpServerRouterRule>(
    "/<arg>/<arg>/log",
    [] (QRegularExpressionMatch &match,
        const QHttpServerRequest &request,
        QHttpServerResponder &&responder) {
});

router.addRule<ViewHandler>(std::move(rule));

void QHttpServerRouter::clearConverters()

すべてのコンバータを削除します。

注: clearConverters () はデフォルトのコンバータを設定しません。

converters およびaddConverterも参照してください 。

QHash<QMetaType, QString> QHttpServerRouter::converters() &&

const QHash<QMetaType, QString> &QHttpServerRouter::converters() const &

このQHttpServerRouter に登録されているコンバータ型と正規表現のマップを返します。これらは、QHttpServerRouterRules のパスパターンで使用できる型です。

デフォルトでは、以下のコンバータが利用可能です:

定数説明
QMetaType::Int 
QMetaType::Long 
QMetaType::LongLong 
QMetaType::Short 
QMetaType::UInt 
QMetaType::ULong 
QMetaType::ULongLong 
QMetaType::UShort 
QMetaType::Double 
QMetaType::Float 
QMetaType::QString 
QMetaType::QByteArray 
QMetaType::QUrl 
QMetaType::Void空のコンバータ。

addConverter およびclearConvertersも参照してください 。

bool QHttpServerRouter::handleRequest(const QHttpServerRequest &request, QHttpServerResponder &responder) const

responder を使用して、HTTP サーバー向けの新しいrequest をそれぞれ処理します。

ルールの一覧を順に調べ、最初に一致するルールを見つけると、そのルールを実行してtrue を返します。リクエストに一致するルールがない場合は、false を返します。

void QHttpServerRouter::removeConverter(QMetaType metaType)

型「metaType 」のコンバータを削除します。

「addConverter」も参照してください 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.