QML 上級チュートリアル 1 - ゲームキャンバスとブロックの作成
アプリケーション画面の作成
まず、アプリケーションの基本的な QML アイテムを作成します。
まず、次のようなメイン画面を持つ「Same Game」アプリケーションを作成します。

これは、次のようなメインアプリケーションファイル `samegame.qml` によって定義されます。
import QtQuick
Rectangle {
id: screen
width: 490; height: 720
SystemPalette { id: activePalette }
Item {
width: parent.width
anchors { top: parent.top; bottom: toolBar.top }
Image {
id: background
anchors.fill: parent
source: "pics/background.jpg"
fillMode: Image.PreserveAspectCrop
}
}
Rectangle {
id: toolBar
width: parent.width; height: 30
color: activePalette.window
anchors.bottom: screen.bottom
Button {
anchors { left: parent.left; verticalCenter: parent.verticalCenter }
text: "New Game"
onClicked: console.log("This doesn't do anything yet...")
}
Text {
id: score
anchors { right: parent.right; verticalCenter: parent.verticalCenter }
text: "Score: Who knows?"
}
}
}これにより、ブロック用のメインキャンバス、「New Game」ボタン、およびスコア表示を含む基本的なゲームウィンドウが作成されます。
ここで見慣れないアイテムとして、「SystemPalette 」があります。これは Qt システムパレットへのアクセスを提供し、ボタンによりネイティブな外観と操作感を与えるために使用されます。
Item 、Button 、Text の各タイプに対するアンカーは、可読性を高めるためにグループ(ドット)表記で設定されている点に注目してください。
Button およびBlock コンポーネントの追加
上記のコードにあるButton アイテムは、Button.qml という名前の別のコンポーネントファイルで定義されています。機能するボタンを作成するために、Rectangle 内でQML型のText とMouseArea を使用します。以下にButton.qml のコードを示します:
import QtQuick
Rectangle {
id: container
property string text: "Button"
signal clicked
width: buttonLabel.width + 20; height: buttonLabel.height + 5
border { width: 1; color: Qt.darker(activePalette.button) }
antialiasing: true
radius: 8
// color the button with a gradient
gradient: Gradient {
GradientStop {
position: 0.0
color: {
if (mouseArea.pressed)
return activePalette.dark
else
return activePalette.light
}
}
GradientStop { position: 1.0; color: activePalette.button }
}
MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: container.clicked();
}
Text {
id: buttonLabel
anchors.centerIn: container
color: activePalette.buttonText
text: container.text
}
}これは基本的に、テキストを含み、クリック可能な矩形を定義しています。MouseArea にはonClicked() ハンドラが設定されており、この領域がクリックされた際に、container のclicked() シグナルを発信するように実装されています。
『Same Game』では、ゲーム開始時に画面が小さなブロックで埋め尽くされます。各ブロックは、単に画像を含むアイテムに過ぎません。ブロックのコードは、別のBlock.qml ファイルで定義されています:
import QtQuick
Item {
id: block
Image {
id: img
anchors.fill: parent
source: "pics/redStone.png"
}
}現時点では、ブロックは何もせず、単なる画像に過ぎません。チュートリアルが進むにつれて、ブロックにアニメーションや動作を追加していきます。ブロックを作成するためのコードはまだ追加していません。これについては次の章で行います。
anchors.fill: parent を使用して、画像のサイズを親アイテムのサイズに合わせて設定しました。これにより、チュートリアルの後半でブロックアイテムを動的に作成・サイズ変更した際、画像は自動的に適切なサイズにスケーリングされます。
Image型のsource プロパティの相対パスに注目してください。このパスは、Image 型を含むファイルの場所を基準としています。あるいは、Imageのソースを、画像を含む絶対ファイルパスやURLに設定することも可能です。
ここまでのコードについては、もうお分かりいただけたと思います。まずは始めとして、いくつかの基本的な型を作成しました。次に、ゲームキャンバスにいくつかのブロックを配置していきます。
© 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.