Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Using objects id in another QML file
Forum Updated to NodeBB v4.3 + New Features

Using objects id in another QML file

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 5 Posters 3.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C CrowSun

    Hello everyone, I'm learning mechanics of QQmlComponent and in that process I encountered a problem.
    I have such code in AppleType.qml

    import 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?

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by raven-worx
    #2

    @CrowSun
    for your case you can use the Window attached property Window.window:

    MouseArea {
         ...
         drag.maximumX: Window.window.width - appleImage.width
         drag.maximumY: Window.window.height - appleImage.height
    }
    

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    C 1 Reply Last reply
    0
    • raven-worxR raven-worx

      @CrowSun
      for your case you can use the Window attached property Window.window:

      MouseArea {
           ...
           drag.maximumX: Window.window.width - appleImage.width
           drag.maximumY: Window.window.height - appleImage.height
      }
      
      C Offline
      C Offline
      CrowSun
      wrote on last edited by
      #3

      @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
                  }
      }
      
      raven-worxR 1 Reply Last reply
      0
      • C CrowSun

        @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
                    }
        }
        
        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #4

        @CrowSun
        how do you create the AppleType element exactly?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        C 1 Reply Last reply
        0
        • raven-worxR raven-worx

          @CrowSun
          how do you create the AppleType element exactly?

          C Offline
          C Offline
          CrowSun
          wrote on last edited by
          #5

          @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();
          }
          
          
          timdayT 1 Reply Last reply
          0
          • C CrowSun

            @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();
            }
            
            
            timdayT Offline
            timdayT Offline
            timday
            wrote on last edited by
            #6

            @CrowSun Curious... why does AppleType need its own QmlEngine? Why can't you just create an AppleType instance within the main.qml ?

            JKSHJ C 2 Replies Last reply
            0
            • timdayT timday

              @CrowSun Curious... why does AppleType need its own QmlEngine? Why can't you just create an AppleType instance within the main.qml ?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #7

              @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.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              3
              • timdayT timday

                @CrowSun Curious... why does AppleType need its own QmlEngine? Why can't you just create an AppleType instance within the main.qml ?

                C Offline
                C Offline
                CrowSun
                wrote on last edited by
                #8

                @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

                timdayT JKSHJ 2 Replies Last reply
                0
                • C CrowSun

                  @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

                  timdayT Offline
                  timdayT Offline
                  timday
                  wrote on last edited by
                  #9

                  @CrowSun You can create (and destroy) objects dynamically from QML no problem (using javascript fragments; no C++ needed)... see http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html . It works very well.

                  1 Reply Last reply
                  2
                  • C CrowSun

                    @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

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #10

                    @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.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    C GrecKoG 2 Replies Last reply
                    2
                    • JKSHJ JKSH

                      @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.

                      C Offline
                      C Offline
                      CrowSun
                      wrote on last edited by
                      #11

                      @JKSH @timday thank you! I'll check that examples. But I have one extremely silly question. If I want to be a c++/qt programmer, than I should learn JS, cause they are inseparable, am I right?

                      1 Reply Last reply
                      0
                      • JKSHJ JKSH

                        @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.

                        GrecKoG Offline
                        GrecKoG Offline
                        GrecKo
                        Qt Champions 2018
                        wrote on last edited by
                        #12

                        @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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved