# iXWPKnopf.qml: import QtQuick 2.0 Rectangle { width: 300 height: 300 color: "steelblue" } # main.qml: ApplicationWindow { title: qsTr("Hello World") width: 640 height: 480 visible: true iXWPKnopf{} } # Listing 1: Zweite Version von iXWPKnopf.qml Rectangle { width: 300 height: 300 color: "steelblue" property alias text:myField.text Text{ id: myField text: "Hallo iX!" anchors.verticalCenter: parent.verticalCenter anchors.horizontalCenter: parent.horizontalCenter } } # main.qml: ApplicationWindow { . . . iXWPKnopf{ text:"Änderung erfolgreich!" } } # Listing 2: iXWPKnopf.qml – inneres Rectangle aus äußerem abgeleitet Rectangle { width: 300 height: 300 color: "steelblue" property alias text:myField.text Rectangle{ width: parent.width-20 height: parent.height-20 anchors.verticalCenter: parent.verticalCenter anchors.horizontalCenter: parent.horizontalCenter color: "lightblue" Text{ id: myField text: "Hallo iX!" anchors.verticalCenter: parent.verticalCenter anchors.horizontalCenter: parent.horizontalCenter } } } # Listing 3: iXWPKnopf.qml – inneres Rectangle mit Ankern versehen Rectangle { width: 300 height: 300 color: "steelblue" property alias text:myField.text Rectangle{ anchors.left: parent.left anchors.leftMargin: 5 anchors.right: parent.right anchors.rightMargin: 15 anchors.top: parent.top anchors.topMargin: 5 anchors.bottom: parent.bottom anchors.bottomMargin: 15 color: "lightblue" Text{ id: myField text: "Hallo iX!" anchors.verticalCenter: parent.verticalCenter anchors.horizontalCenter: parent.horizontalCenter } } } # iXWPKnopf.qml: Rectangle { . . . MouseArea{ anchors.fill: parent onClicked: parent.text="Autsch!" } } # iXWPKnopf.qml: MultiPointTouchArea { anchors.fill: parent touchPoints: [ TouchPoint { id: point1 }, TouchPoint { id: point2 } ] } # Listing 4: iXWPKnopf.qml – Pinch-Handler onPinchStarted: { initialWidth = flick.contentWidth initialHeight = flick.contentHeight } onPinchUpdated: { //Drag flick.contentX += pinch.previousCenter.x - pinch.center.x flick.contentY += pinch.previousCenter.y - pinch.center.y //Resize flick.resizeContent(initialWidth * pinch.scale, initialHeight * pinch.scale, pinch.center) } onPinchFinished: {} # Listing 5: iXWPKnopf.qml – Animationselemente Rectangle { . . . Rectangle{ id:innerRect . . . } MouseArea{ anchors.fill: parent onClicked: parent.text="Autsch!" onPressed:animateTo.start() onReleased: animateFrom.start() } PropertyAnimation {id: animateTo; target: innerRect; properties: "color"; to: "red"; duration: 100} PropertyAnimation {id: animateFrom; target: innerRect; properties: "color"; to: "lightblue"; duration: 100} } # Listing 6: main.qml – Button in Grid einfügen ApplicationWindow { . . . Grid { anchors.fill: parent columns: 2 rows: 3 spacing: 10 Repeater { model: 6 iXWPKnopf{ text:"Änderung erfolgreich!" width: (parent.width - parent.spacing*(parent.columns-1))/ parent.columns height: (parent.height -parent.spacing*(parent.rows-1)) / parent.rows } } } } # C++-Hilfsklasse iXHelper: #include class iXHelper : public QObject { Q_OBJECT . . . public slots: void workIt(int _whichID); }; void iXHelper::workIt(int _whichID) { qDebug() << "Auslöser: " << _whichID; } # Listing 7: main.qml – Repeater-Sammelstelle ApplicationWindow { . . . id: materForm signal clickOccurred(int _which) Grid . . . Repeater { objectName: "myRepeater" model: 6 iXWPKnopf{ mynumber: index . . . onButtonClicked: materForm.clickOccurred(mynumber) } } } } # iXWPKnopf.qml: Rectangle { property int mynumber:-1 signal buttonClicked(int zahl) . . . MouseArea{ anchors.fill: parent onClicked: { parent.text="Autsch!"; parent.buttonClicked(mynumber); } } } # Listing 8: main() int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); iXHelper *myHelper=new iXHelper(); QObject::connect(engine.rootObjects().first(), SIGNAL(clickOccurred(int)),myHelper,SLOT(workIt(int))); return app.exec(); } # main.qml: ApplicationWindow { . . . signal clickOccurred(int _which) function touchAButton(_which) { myGrid.children[_which].text="XXX"; } Grid { id: myGrid } } # Listing 9: QTimer in iXHelper.cpp iXHelper::iXHelper(QObject *parent) : QObject(parent) { QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1000); } void iXHelper::update() { emit touchAButton(QVariant(3)); } ... #include <\#60>QVariant> class iXHelper : public QObject { . . . signals: void touchAButton(QVariant _which); public slots: void update(); }; # main() QObject::connect(myHelper, SIGNAL (touchAButton(QVariant)),engine.rootObjects().first(), SLOT(touchAButton(QVariant))); # Listing 10: kleiner „Parser“ QString makeDepth(int _how) { QString x=""; for(int i=0;i<_how;i++) { x+="xx"; } return x; } void recursiveParse(QObject *_item, int depthLevel) { if(depthLevel==0) { qDebug() << _item->objectName() << " of " << _item->metaObject()->className(); } QObjectList myList=_item->children(); for(int i=0;iobjectName() << " of " << innerElement->metaObject()->className(); if(innerElement->children().count()!=0) { recursiveParse(innerElement, depthLevel+1); } } } # Direktzugriff QObject *rect = object->findChild("rect"); if (rect) rect->setProperty("color", "red");