大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

Qt – Androidアプリ開発 – property alias

株式会社クローバーフィールドの経営理念
著者:津路高広
公開日:2020/02/25
最終更新日:2020/02/29
カテゴリー:技術情報
タグ:

津路です。
前回Qt – Androidアプリ開発 – バブルの動き、バブルの動きなどを定義しましたが、次は、ビルド、実機で動かす段階です。
プロジェクトをオープンして、main.qml内での以下の行で、エラーが起きていました。
Behavior on y {
yは、不可解な定義だよというエラーです。
コードを入れる場所が間違っていたので、Page1Formの中に移動しました。

        Page1Form {
            bubble {
                id: bubble
                centerX: mainWindow.width / 2
                centerY: mainWindow.height / 2
                bubbleCenter: bubble.width / 2
                x: bubble.centerX - bubble.bubbleCenter
                y: bubble.centerY - bubble.bubbleCenter
                Behavior on y {
                    SmoothedAnimation {
                        easing.type: Easing.Linear
                        duration: 100
                    }
                }
                Behavior on x {
                    SmoothedAnimation {
                        easing.type: Easing.Linear
                        duration: 100
                    }
                }
            }
        }

さて、ビルドはでき、実機をつないで実行すると、画面は出ますが、中身がなくて落ちます。
アプリケーション出力タブに以下のエラーが表示されてます。
W libfirst-sample.so: qrc:/main.qml:15 Type Page1Form unavailable
W libfirst-sample.so: qrc:/Page1Form.ui.qml:7 Invalid alias reference. Unable to find id “bubble”
調べると、bubbleへのエイリアスが必要なようです。
Page1Form.ui.qmlをjavascriptエディタで開いて、以下のようにエイリアスを定義します。

Page {
    width: 600
    height: 400
    property alias bubble: bubble
    property alias mainWindow: mainWindow

そして、よく見ると、bubbleがRectangleの上にないので、以下のように訂正します。

Page {
    width: 600
    height: 400
    property alias mainWindow: mainWindow
    property alias bubble: bubble

    header: Label {
        text: qsTr("Page 1")
        font.pixelSize: Qt.application.font.pixelSize * 2
        padding: 10
    }

    Rectangle {
        id: mainWindow
        color: "#ffffff"
        anchors.fill: parent

        Bubble {
            id: bubble
        }
    }
}

さて、再度ビルド、実行すると、また以下のエラーで落ちました。
W libfirst-sample.so: qrc:/main.qml:9 Invalid alias reference. Unable to find id “mainWindow”
おかしいなあと思って、id:mainWindowを捜すと、ありました。勘違いして、exportしたときに、bubble.qmlのほうへ書いていました。
bubble.qmlには、Rectangleもidも不要です。

Image {
    source: "Bluebubble.svg"
    smooth: true
    property real centerX
    property real centerY
    property real bubbleCenter
}

はてさて、実行すると、まだよく似たエラー。
W libfirst-sample.so: qrc:/main.qml:66: ReferenceError: mainWindow is not defined
main.qmlのAccelerometer定義内で、起こっています。
どうも、mainWindowを、デザイナーモードでexportしても、外部のmain.qmlから参照できない不具合です。
ApplicationWindow内で、Page1Formをexportして、mainWindowの個所をpage1Formに差し替えると、見事にバブルが表示されました。

    上に戻る