3

I need an advice about my simple QT/QML application.

I have the following situation:

about the interface, I have a main area called 'flickableArea' (flickableArea.qml) divided into four areas (item slot1, item slot2, item slot3 and item slot4).

Each slot is filled with QML Rectangle object.

slot1 filled by rectangle with id=area1, slot2 filled by rectangle with id=area2, slot3 filled by rectangle with id=area3, slot4 filled by rectangle with id=area4

FILE flickableArea.qml

import QtQuick 2.4
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4

Item {

    property alias mainGrid: mainGrid
    property alias slot1: slot1
    property alias slot2: slot2
    property alias slot3: slot3
    property alias slot4: slot4

    id: flickableAreaItem
    width: 600
    height: 300

    Flickable {
        id: flickableArea
        boundsBehavior: Flickable.DragOverBounds
        interactive: true
        flickableDirection: Flickable.HorizontalFlick
        anchors.fill: parent

        GridLayout {
            id: mainGrid
            columnSpacing: 3
            rowSpacing: 3
            rows: 2
            columns: 2
            anchors.fill: parent

            Item {
                id: slot1
                Layout.fillWidth: true
                Layout.fillHeight: true
                clip: false
                Rectangle {
                    anchors.fill: parent
                    border.width: 1
                    border.color: "black"
                }
            }

            Item {
                id: slot2
                Layout.fillWidth: true
                Layout.fillHeight: true
                clip: false
                Rectangle {
                    anchors.fill: parent
                    border.width: 1
                    border.color: "black"
                }
            }

            Item {
                id: slot3
                Layout.fillWidth: true
                Layout.fillHeight: true
                clip: false
                Rectangle {
                    anchors.fill: parent
                    border.width: 1
                    border.color: "black"
                }
            }

            Item {
                id: slot4
                Layout.fillWidth: true
                Layout.fillHeight: true
                clip: false
                Rectangle {
                    anchors.fill: parent
                    border.width: 1
                    border.color: "black"
                }
            }

        }
    }
}

I should insert dynamically QML objects in area1, area2, area3 and area4 when a defined signal is triggered in the c++ code. On c++ code, when the signal is triggered, I run the following code to create a new object (ObjectToIntroduce) connected to Object.qml:

ObjectToIntroduce *obj;
obj = new ObjectToIntroduce();
QQmlContext *objContext = engine->rootContext();
objContext->setContextProperty("obj", obj);

After I have made a new ObjectToIntroduce how can I introduce/destroy (view/hide) Object.qml in area1/area2/area3/area4 of flickableArea.qml?

I want to know what is the best way to implement this mechanism, to writing this simple Qt/Qml application. Thanks in advice, best regards

Lele

daniele86
  • 147
  • 3
  • 14
  • why cant you use "Connections" in qml and "target" as obj, then handle hide/show in the connection slot ."http://doc.qt.io/qt-5/qtqml-syntax-signals.html" – Ashif Jun 08 '17 at 05:38
  • 2
    Creating QML components in C++ isn't good practice. Instead of that you can implement your own component derived from `QQuickItem` and so create it in QML in regular way. As for your question, you should provide more code. What is `ObjectToIntroduce`? – folibis Jun 08 '17 at 06:19
  • ObjectToIntroduce is a simple c++ class that have Q_PROPERTY (for example const QString) connected to the Object.qml. I should insert this Object.qml in flickableArea.qml when the signal is triggered in the c++ code. Thanks – daniele86 Jun 08 '17 at 06:30
  • For example I can create a QQmlComponent from c++ code, but I don't know how load this component in other qml object. – daniele86 Jun 08 '17 at 09:59

0 Answers0