2018年3月28日

Instantiator

モジュール

QtQml 2.1

クラス継承

QQmlInstantiator → QObject

プロパティー

active: bool = true
asynchronous: bool = false
count: int = 0 readonly
delegate: Component = null default
model: QVariant = 1
object: QtObject = null readonly

シグナル

objectAdded(int index, QtObject object)
objectRemoved(int index, QtObject object)
activeChanged()
asynchronousChanged()
countChanged()
delegateChanged()
modelChanged()
objectChanged()

メソッド

QtObject objectAt(int index)

説明


delegate に設定されたコンポーネントのオブジェクトを生成する。コンポーネントはビジュアルと非ビジュアルのどちらでもよい。

生成オブジェクトの親 QObject は Instantiator になる。ビジュアルオブジェクトを生成したときにはその parent は null になる。
active.qml:

import QtQuick 2.10
import QtQuick.Window 2.10

Window {
    id: window

    visible: true

    width: 300; height: 300

    Instantiator {
        id: generator

        model: [ "red", "green", "blue", "yellow", "purple" ]
        active: false

        Rectangle {
            x: 25 + index*width; y: x
            width: 50; height: width

            color: modelData

            Component.onCompleted: {
                print("ctor", this, this.color);
            }

            Component.onDestruction: {
                print("dtor", this, this.color);
            }
        }

        onObjectAdded: {
            object.parent = window.contentItem;
        }
    }

    MouseArea {
        anchors.fill: parent

        onClicked: {
            generator.active = !generator.active;
        }
    }
}
オブジェクト追加時に parent を設定して表示されるようにしている。この設定をしないと表示されない。実行すると以下のようになり、active の切り替えでオブジェクトの生成と破壊がされているのがわかる。
qml: ctor QQuickRectangle(0x7fed5de5a300) #ff0000
qml: ctor QQuickRectangle(0x7fed5de5d540) #008000
qml: ctor QQuickRectangle(0x7fed5de5e3e0) #0000ff
qml: ctor QQuickRectangle(0x7fed5de5f280) #ffff00
qml: ctor QQuickRectangle(0x7fed5de60120) #800080
qml: dtor QQuickRectangle(0x7fed5de5a300) #ff0000
qml: dtor QQuickRectangle(0x7fed5de5d540) #008000
qml: dtor QQuickRectangle(0x7fed5de5e3e0) #0000ff
qml: dtor QQuickRectangle(0x7fed5de5f280) #ffff00
qml: dtor QQuickRectangle(0x7fed5de60120) #800080

プロパティーの説明

active: bool

true のときに delegate にコンポーネントが設定されていて status が Component.Ready ならばモデルに対してオブジェクトを生成する。false ならばオブジェクトは生成せず、生成オブジェクトがあれば破壊する。

asynchronous: bool

true ならば非同期にオブジェクトを生成する。

count: int

生成し抱えているオブジェクト数。

delegate: Component

オブジェクト生成に使われるコンポーネント。

生成オブジェクトには読込み専用プロパティー index に生成順が設定される。model が、整数、配列、文字列リスト、オブジェクトリストの場合には読込み専用プロパティー modelData に要素 (整数の場合には生成順) が設定される。

model: QVariant

以下のいずれかを設定する。整数を指定した場合にはその数分のオブジェクトが生成され、他の場合には各要素に対してオブジェクトが生成される。
  • 整数
  • 配列
  • 文字列リスト
  • オブジェクトリスト
  • ObjectModel や ListModel、QAbstractItemModel 継承モデルなど
配列を設定した場合に model を参照すると配列への参照が返される。

配列を設定し、同じ配列を再設定した場合でもオブジェクトの削除と生成がされる。

object: QtObject

生成したオブジェクトがあれば 1 番目に生成したオブジェクトへの参照が設定される。オブジェクトをひとつしか生成しない場合にオブジェクトの参照を簡単にするために用意されている。

シグナルの説明

objectAdded(int index, QtObject object)

オブジェクトが追加されたときに送信されるシグナル。index は追加位置で object は追加されたオブジェクト。

objectRemoved(int index, QtObject object)

オブジェクトを削除しようとするときに送信されるシグナル。index は削除位置で object は削除しようとするオブジェクト。シグナルハンドラー onObjectRemoved() を抜けた直後にオブジェクトが削除される。

active を false にしたときには objectRemoved() の中では count は生成して抱えたオブジェクト数のままで、全オブジェクトの削除後に count が 0 になる。

メソッドの説明

QtObject objectAt(int index)

index で指定した位置のオブジェクトへの参照を返す。

注意


model を参照したときの動作が Repeater の model とは異なっている。

参考情報

Instantiator QML Type
Repeater

大域関数

モジュール

QtQml 2.3

関数

string QT_TRANSLATE_NOOP(string context, string sourceText, string disambiguation)
string QT_TRID_NOOP(string id)
string QT_TR_NOOP(string sourceText, string disambiguation)
string qsTr(string sourceText, string disambiguation, int n)
string qsTrId(string id, int n)
string qsTranslate(string context, string sourceText, string disambiguation, int n)
gc()
print(...) 

説明


プリフィックスなしで直接呼び出せる大域関数。

関数の説明

string QT_TRANSLATE_NOOP(string context, string sourceText, string disambiguation)

string QT_TRID_NOOP(string id)

string QT_TR_NOOP(string sourceText, string disambiguation)

string qsTr(string sourceText, string disambiguation, int n)

string qsTrId(string id, int n)

string qsTranslate(string context, string sourceText, string disambiguation, int n)

gc()

ガーベージコレクションを作動する。

print(...)

console.log() と同一。

参考情報

utils.js

共通 JavaScript 変数と関数


utils.js:

.pragma library

const trolltechGreen = Qt.rgba(141/255, 192/255, 48/255)
const trolltechPurple = Qt.rgba(131/255, 124/255, 172/255)

const buttonTextColor = Qt.rgba(0.0, 0.47843137254902, 1.0, 1.0)

function colorName(color) {
    const tos = Object.prototype.toString;

    function formatElement(e) {
        return ("00" + Math.round(255*e).toString(16)).slice(-2);
    }

    const typeString = tos.call(color);
    const typeName = typeString.match(/[a-z]+/gi)[1];
    if (typeName === "String") {
        return "";
    }

    const r = formatElement(color.r);
    const g = formatElement(color.g);
    const b = formatElement(color.b);
    const a = formatElement(color.a);

    return "#%1%2%3%4".arg(a).arg(r).arg(g).arg(b);
}

const monoFont = function() {
    if (Qt.platform.os == "linux") {
        return Qt.font({ family: "DejaVuSansMono" });
    } else if (Qt.platform.os == "windows") {
        return Qt.font({ family: "Lucida Console" });
    } else if (Qt.platform.os == "osx") {
        return Qt.font({ family: "Osaka", styleName: "Regular-Mono" });
    } else {
        return Qt.font({ family: "Lucida Console" });
    }
}();

function grayFor(color) {
    return ((255 * color.r * 11 + 255 * color.g * 16 + 255 * color.b * 5) / 32) / 255;
}

function textColorFor(aBackgroundColor) {
    return grayFor(aBackgroundColor) > 0.5 ? "black" : "white";
}

function randomNumberBetween(first, last) {
    return first + Math.floor(Math.random() * (last - first + 1));
}

function classNameOf(object) {
    return object.toString().replace(/\(.*$/, "");
}

var qtVersionNumber = function() {
    const component = Qt.application.version.split(".");
    return Number(component[0]+ ("0" + component[1]).substring(-2) + ("0" + component[2]).substring(-2));
}();

function dumpObjectTree(object) {
    function traverse(object, level) {
        if (!object.children) {
            return;
        }

        for (var index in object.children) {
            var line = "";
            for (var _ = 0; _ < level; ++_) {
                line += "    ";
            }
            const child = object.children[index];
            line += classNameOf(child);
            if (child.objectName) {
                line += " (%1)".arg(child.objectName);
            }
            print(line)
            traverse(child, level + 1);
        }
    }

    traverse(object, 0);
}

説明


変数の説明

trolltechGreen

Trolltech の緑色。

trolltechPurple

Trolltech の紫色。

buttonTextColor

ボタンテキスト色。

monoFont

各プラットフォームでの代表的な固定幅フォント名。

qtVersionNumber

Qt のバージョンナンバーを MNNPP 形式の整数で保持する。Qt 5.8.1 ならば 50801、Qt 5.9 ならば 50900。

関数の説明

String colorName(color color)

色名のアルファ値を省略せずに常に #aarrggbb 形式の文字列で返す。

Number grayFor(color color)

指定された色をグレイ化する。Qt C++ の qGray() と同じ。

String textColorFor(color aBackgroundColor)

指定された背景色に対してはっきり見えるテキスト色を "white" か "black" で返す。

Number randomNumberBetween(int first, int last)

first を下限、last を上限として乱数を返す。

String classNameOf(QObject object)

QML オブジェクトの C++ クラス名を返す。

dumpObjectTree(object)

ビジュアルツリー構造を表示する。