Using objects id in another QML file
-
Hello everyone, I'm learning mechanics of QQmlComponent and in that process I encountered a problem.
I have such code in AppleType.qmlimport QtQuick 2.0 Image { id: appleImage source: "qrc:/images/Apple.png" MouseArea { id: appleImageDragArea anchors.fill: parent drag.target: appleImage drag.axis: Drag.XAndYAxis drag.minimumX: 0 drag.maximumX: myWindow.width - appleImage.width drag.minimumY: 0 drag.maximumY: myWindow.height - appleImage.height } }
and in "main.qml" I have that one:
import QtQuick 2.7 import QtQml 2.0 import QtQuick.Window 2.2 import QtQuick.Controls 1.2 Window { id: myWindow visible: true width: 800 height: 600 title: qsTr("Grab a pic v1.0") color:"LightGreen" }
But I get an error "ReferenceError: myWindow is not defined".
I definitely need that id, to specify sizes of dragable area. Is that possible to do? -
@CrowSun
for your case you can use the Window attached propertyWindow.window
:MouseArea { ... drag.maximumX: Window.window.width - appleImage.width drag.maximumY: Window.window.height - appleImage.height }
-
@raven-worx Thank you for replying!
I replaced maximumX and maximumY with your example and now I get that one error:
"TypeError: Cannot read property 'width' of null"
and the same for "height".
Thats how my qml file looks now:import QtQuick 2.0 import QtQuick.Window 2.2 Image { id: appleImage source: "qrc:/images/Apple.png" MouseArea { id: appleImageDragArea anchors.fill: parent drag.target: appleImage drag.axis: Drag.XAndYAxis drag.minimumX: 0 drag.maximumX: Window.window.width - appleImage.width drag.minimumY: 0 drag.maximumY: Window.window.height - appleImage.height } }
-
@CrowSun
how do you create theAppleType
element exactly? -
@raven-worx I'm sorry, I didn't notice that someone replied to me.
I create my AppleItem in cpp file and that is how it looks like:int main(int argc, char *argv[]) { #if defined(Q_OS_WIN) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; QQmlEngine engineForPic; QQmlComponent component(&engineForPic, QUrl(QStringLiteral("qrc:/AppleType.qml"))); QObject *object = component.create(); return app.exec(); }
-
@timday said in Using objects id in another QML file:
@CrowSun Curious... why does AppleType need its own QmlEngine? Why can't you just create an AppleType instance within the main.qml ?
To expand @timday's comment: QML code that is loaded by 1 engine cannot see QML coded that is loaded by a different engine. You must create an
AppleType
object inside main.qml itself. -
@timday, @JKSH I started to code all this weird things because I need to create dinamically qml objects in window. For example, I need to create random number of small pictures in window that should be positioned in random X and Y. As I'm extremely new to QML+Qt I tried to figure out how it could be done and then found an article in qml documentation about QmlEngine. That's how I decide to make an QmlEngine only for one Image. I'm feeling that I'm sooo far from right decision... If you had some advices on how it should be done, I'll be very thanksful
-
@CrowSun said in Using objects id in another QML file:
I started to code all this weird things because I need to create dinamically qml objects in window.
Open Qt Creator and search for the example called "QML Example - Dynamic Scene". It shows you how to dynamically create new Items using JavaScript. It's quite a complex example, but you can study it by modifying the code.
By the way, I re-read your posts again and I realized an important issue: A QML id can only be accessed from a single QML file. You cannot use an object's ID in a different file.
-
@JKSH said in Using objects id in another QML file:
By the way, I re-read your posts again and I realized an important issue: A QML id can only be accessed from a single QML file. You cannot use an object's ID in a different file.
You can use an id from an ancestor file, not that I advise it.