How do you call a function in a loader
-
I have a loader in my main.qml, and I have a ComboBox, I want to be able to call a function in ComboBox via a loader, but I cannot seem to find a way to do this, without a loader it works fine, it is the loader that I do not get, I use a menu to call the loader, I use the loader to change languages in my app, this I just a short example.
import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.15 ApplicationWindow { width: 640 height: 480 visible: true title: qsTr("Test Loader ComboBox") /************************************************ * @brief MenuBar. * MenuBar ***********************************************/ menuBar: MenuBar { id: myMenuBar /************************************************ * @brief MenuBar File. * MenuBar ***********************************************/ Menu { id: myMenuFile title: qsTr("&File") MenuItem { text: qsTr("&First") onTriggered: { theFindComboBox.setComboBox("First"); thisComboBoxLoader.setComboBox("First"); } } MenuItem { text: qsTr("&Second") onTriggered: { theFindComboBox.setComboBox("Second"); MyComboBox.setComboBox("Second"); } } MenuItem { text: qsTr("&Third") onTriggered: { theFindComboBox.setComboBox("Third"); loaderContainer.setComboBox("Third"); } } MenuItem { text: qsTr("&reload") onTriggered: { thisComboBoxLoader.reload(); } } MenuItem { text: qsTr("&Quit") onTriggered: Qt.quit() } } // end myMenuFile } // end MenuBar /************************************************ * @brief MyComboBox theFindComboBox. * MyComboBox ***********************************************/ MyComboBox { id: theFindComboBox } /************************************************ * @brief Item for Loader. * Item ***********************************************/ Item { id: loaderContainer y: 100 Loader { id: thisComboBoxLoader /************************************************ * @brief thisComboBoxLoader.reload(). * reload ***********************************************/ function reload() { source = ""; source = "MyComboBox.qml"; console.debug("Reloaded") } anchors.centerIn: parent source: "MyComboBox.qml" } // end Loader } } // end Item /******************************* End of File *********************************/
And I have a ComboBox with a function to change the index
import QtQuick 2.0 import QtQuick.Controls 2.15 import QtQml 2.15 Item { id: theFindThisComboBox property alias thisComboBox: theComboBox ComboBox { id: theComboBox model: ListModel { id: theModel ListElement { text: "First" } ListElement { text: "Second" } ListElement { text: "Third" } } } function setComboBox(findThis) { thisComboBox.currentIndex = thisComboBox.find(findThis); } Component.onCompleted: { setComboBox("Second"); } } /******************************* End of File *********************************/
Thanks Flesh
-
I have a loader in my main.qml, and I have a ComboBox, I want to be able to call a function in ComboBox via a loader, but I cannot seem to find a way to do this, without a loader it works fine, it is the loader that I do not get, I use a menu to call the loader, I use the loader to change languages in my app, this I just a short example.
import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.15 ApplicationWindow { width: 640 height: 480 visible: true title: qsTr("Test Loader ComboBox") /************************************************ * @brief MenuBar. * MenuBar ***********************************************/ menuBar: MenuBar { id: myMenuBar /************************************************ * @brief MenuBar File. * MenuBar ***********************************************/ Menu { id: myMenuFile title: qsTr("&File") MenuItem { text: qsTr("&First") onTriggered: { theFindComboBox.setComboBox("First"); thisComboBoxLoader.setComboBox("First"); } } MenuItem { text: qsTr("&Second") onTriggered: { theFindComboBox.setComboBox("Second"); MyComboBox.setComboBox("Second"); } } MenuItem { text: qsTr("&Third") onTriggered: { theFindComboBox.setComboBox("Third"); loaderContainer.setComboBox("Third"); } } MenuItem { text: qsTr("&reload") onTriggered: { thisComboBoxLoader.reload(); } } MenuItem { text: qsTr("&Quit") onTriggered: Qt.quit() } } // end myMenuFile } // end MenuBar /************************************************ * @brief MyComboBox theFindComboBox. * MyComboBox ***********************************************/ MyComboBox { id: theFindComboBox } /************************************************ * @brief Item for Loader. * Item ***********************************************/ Item { id: loaderContainer y: 100 Loader { id: thisComboBoxLoader /************************************************ * @brief thisComboBoxLoader.reload(). * reload ***********************************************/ function reload() { source = ""; source = "MyComboBox.qml"; console.debug("Reloaded") } anchors.centerIn: parent source: "MyComboBox.qml" } // end Loader } } // end Item /******************************* End of File *********************************/
And I have a ComboBox with a function to change the index
import QtQuick 2.0 import QtQuick.Controls 2.15 import QtQml 2.15 Item { id: theFindThisComboBox property alias thisComboBox: theComboBox ComboBox { id: theComboBox model: ListModel { id: theModel ListElement { text: "First" } ListElement { text: "Second" } ListElement { text: "Third" } } } function setComboBox(findThis) { thisComboBox.currentIndex = thisComboBox.find(findThis); } Component.onCompleted: { setComboBox("Second"); } } /******************************* End of File *********************************/
Thanks Flesh
@Flesh What have you tried, and what are the symptoms of it not working? That example is 131 lines of code, and leaves me wondering what the goal is. To pick a random portion:
function reload() { source = ""; source = "MyComboBox.qml"; console.debug("Reloaded") }
How does setting
Loader.source
twice relate to the topic's question?Something more concise might help.
-
This the working parts of the problem, it works if it is not a loader, but fails to connect as a loader, there must be something I am missing about how the loader works as far as calling functions in it.
Not sure what you mean about the source twice, but that part of the code works, you have to set it to undefined or null to cause a reload to take place, the problem is how do I call a function that is loaded from the source.
-
@Flesh said in How do you call a function in a loader:
thisComboBoxLoader
<id of loader>.item.<function name>()
thisComboBoxLoader.item.reload()
-
@Flesh said in How do you call a function in a loader:
MenuItem { text: qsTr("&First") onTriggered: { theFindComboBox.setComboBox("First"); // **This Works** thisComboBoxLoader.setComboBox("First"); // **I have tired many ways to call this but none work** } }
-
@Flesh said in How do you call a function in a loader:
MenuItem { text: qsTr("&First") onTriggered: { theFindComboBox.setComboBox("First"); // **This Works** thisComboBoxLoader.setComboBox("First"); // **I have tired many ways to call this but none work** } }
Note the
item
difference between the two lines:@Flesh said in How do you call a function in a loader:
thisComboBoxLoader.setComboBox("First");
@fcarney said in How do you call a function in a loader:
thisComboBoxLoader.item.reload()
thisComboBoxLoader
is the Loader.thisComboBoxLoader.item
is the object loaded by the Loader. Properties of the loaded object do not become properties of the Loader.