Dialog defined in other file - error on access
-
Hey mates,
Please give me a hint what's wrong. I have two qml files, main and SettingsDialog, both are in the same folder:
//main.qml import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.5 import QtQuick.Dialogs 1.2 import QtQuick.Layouts 1.3 //import "qrc:/SettingsDialog.qml" ApplicationWindow { id: mainWindow objectName: "Main App Window" width: 240 height: 180 visible: true title: qsTr("Hello World") Keys.onSpacePressed: { settingsBaseDialog.open() } }
//SettingsDialog.qml import QtQuick 2.15 import QtQuick.Controls 2.5 import QtQuick.Dialogs 1.2 Dialog { id: settingsBaseDialog //some stuff here }
and when I press space I got this error message:
qrc:/main.qml:38: ReferenceError: settingsBaseDialog is not definedWhat I'm missing?
-
@MasterBLB said in Dialog defined in other file - error on access:
What I'm missing?
You don't create any instance of SettingsDialog in main.qml, like this:
SettingsDialog { id: settingsBaseDialog }
In general, ids (
id: settingsBaseDialog
and similar calls) are only visible in local scope (in the same file). -
@MasterBLB said in Dialog defined in other file - error on access:
I see...and what in case I need to create SettingDialog on spacebar press, but not earlier?
Either create it hidden with
visible: false
, or useLoader
to load it when you need it.