I'm currently working on a PyQt5 application that incorporates QML. The application dynamically generates QML code from a configuration file (configuration.yaml
). The generated QML code is then sent from Python to QML using signals, and I'm utilizing Qt.createQmlObject()
to instantiate QML objects.
The challenge I'm encountering is that this process works seamlessly in the main application, but when attempting the same in a Popup
element, the generated QML objects aren't visible on the application interface. Notably, no errors are thrown during execution. Despite verifying that the QML objects are successfully created through debugging, they do not appear on the interface.
Since TestPopup.qml
is using MainPopup.qml
as its parent and might be causing a problem. I've checked the addElement()
function and it is working fine.
Code:
MainPopup.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
Popup {
id: mainPopup
...
}
TestPopup.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
MainPopup {
id: testPopup
function addElement(elementId, elementString) {
Qt.createQmlObject(elementString, testPopup, elementId)
}
...
}
app.py
...
test_qml = """
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
Text {
text: "Hello World!"
anchors.centerIn: parent
}
"""
test_popup = window.findChild(QObject, "testPopup")
test_popup.addElement("testID", test_qml)
...
Since, I was trying to add an element in testPopup
which was extending from MainPopup
and was not visible on his own. I had to add the new element to MainPopup
's contentItem
like this:
...
function addElement(elementId, elementString) {
Qt.createQmlObject(elementString, testPopup.contentItem, elementId)
}
...