Reputation: 706
the following code is and illustration for my problem , the qml file im importing and qualifying as EventListner is highlighted in blue , but it doesn't work when i use it.
main.qml:
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.1
import "/qtrealis/untitled15/EventListner.qml" as EventListner
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button{
onClicked:EventListner.color="blue";
}
EventListner.qml:
import QtQuick 2.7
import QtQuick.Window 2.2
Item {
property string color: "dark"
onColorChanged: console.log("event received!")
}
Upvotes: 0
Views: 891
Reputation: 13691
If you want to have singleton instance of the EventListener
you will need to add
pragma Singleton
as first line of your EventListener.qml
pragma Singleton
import QtQuick 2.7
import QtQuick.Window 2.2
Item {
property string color: "dark"
onColorChanged: console.log("event received!")
}
Then you need a file called qmldir
in the directory where you have the EventListener.qml
with the content:
singleton EventListener 1.0 EventListener.qml
Finally you can use it in the main.qml
by importing it:
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.1
import '.' // To import Singletons you need to explicitly import the directory
// that holds the qmldir file
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button {
onClicked: EventListener.color = "blue";
}
}
More on the qmldir
file can be found in the documentation: http://doc.qt.io/qt-5/qtqml-modules-qmldir.html
Note: If you are using the
qrc
-resource system you need to make sure that theqmldir
-file is added to it. (Right-Click on the qml.qrc, Add/Add Existing (depending on whether it is already created)). Otherwise you need to use the import statement:
import 'file:/path/to/the/directory
(Maybe absolute path necessary)
Upvotes: 1