[SOLVED] Add (and change) a component in a placeholder
-
Hi all,
I am quite new to Qt programming, and I think what I try to achieve is quite simple.
The application I am porting uses panels (that contain elements such as buttons and labels). Many of the panels are displayed in the center of the screen, and the application can decide to display one panel or another, depending on several actions. Only one panel is visible at any point of time. I thought that I would implement it the following way:
I would like to define a "placeholder" element in my main QML layout file, let's say:
MainWindow.qml:
@Item {
id: window1
width: 1366
height: 768
objectName: "MainWindow"Item { anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter width: 490 height: 445 objectName: "CenterPanel" }
}@
... And be able, from the C++ code, to dynamically add (and replace) a child to this container placeholder element each time I want to display a panel. This child would simply be a new component, loaded from a QML file depending on the panel that is required to be displayed:
PanelA.qml:
@import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.2Item {
objectName: "PanelA"
anchors.fill: parent...
}@
I expect something like that in my C++ code:
pPanelA = <LOAD PANEL FROM QML FILE>;
pCenterPanel->removeChild();
pCenterPanel->addChild( pPanelA );How can I achieve that?
Thank you.
-
Have you looked at this QML component?
-
I saw this one, but how can I change its source from the C++ code? I haven't found any C++ class that matches the Loader QML type...
-
One way is to emit signal from the C++ code and handle the signal in the QML, although that's a bit different than what you're thinking off.
-
Hi,
If I understood you correctly you will need "qqmlcomponent":http://qt-project.org/doc/qt-5/qqmlcomponent.html to create component from C++ side, then after "beginCreate":http://qt-project.org/doc/qt-5/qqmlcomponent.html#beginCreate cast it to QQuickItem and set it's parent as per your needs.
-
[quote author="p3c0" date="1411450523"]Hi,
If I understood you correctly you will need "qqmlcomponent":http://qt-project.org/doc/qt-5/qqmlcomponent.html to create component from C++ side, then after "beginCreate":http://qt-project.org/doc/qt-5/qqmlcomponent.html#beginCreate cast it to QQuickItem and set it's parent as per your needs.[/quote]
It seems to be what I was looking for. I just figured out the difference between a parent (in the QObject's parent hierarchy) and a visual parent, and thus use setParentItem().
Thank you for your help.