Qt Quick コントロールを始める
コントロールを利用したQMLファイルの基本例を示します:
import QtQuick import QtQuick.Controls ApplicationWindow { title: "My Application" width: 640 height: 480 visible: true Button { text: "Push Me" anchors.centerIn: parent } }
C++からのコントロールの設定
C++アプリケーションでQMLファイルを表示するには、従来からQQuickView が使われてきましたが、これではC++からしかウィンドウのプロパティを設定できません。
Qt Quick コントロールでは、ApplicationWindow をアプリケーションのルートアイテムとして宣言し、代わりにQQmlApplicationEngine を使って起動します。こうすることで、QMLからトップレベルのウィンドウプロパティを制御できるようになります。
以下にコントロールを利用したソースファイルの基本例を示します:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
QMLからC++のデータを利用する
QML から利用する C++ クラスを登録する必要がある場合は、QQmlApplicationEngine を宣言する前にqmlRegisterType() を呼び出すことができます。詳しくは「C++からQMLの型を定義する」を参照してください。
QML コンポーネントにデータを公開する必要がある場合は、現在の QML エンジンのコンテキストからデータを利用できるようにする必要があります。詳しくはQQmlContext を参照してください。
© 2025 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.