Results 1 to 5 of 5

Thread: How to move an QML object from one QML file to another, dynamically?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to move an QML object from one QML file to another, dynamically?

    This is the initial code:

    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Rectangle
    4. {
    5. id: container
    6. height: 300
    7. width: 300
    8.  
    9. WindowA
    10. {
    11. x:0
    12. id: windowAId
    13. color: "green"
    14. }
    15.  
    16. WindowB
    17. {
    18. id: windowBId
    19. anchors.top: windowAId.bottom
    20. anchors.topMargin: 10
    21. color: "black"
    22. }
    23.  
    24. signal completed
    25.  
    26. ListModel {
    27. id: fooModel
    28. ListElement { data: "red" }
    29. }
    30.  
    31. Component.onCompleted: {
    32. console.log("Rect Completed!")
    33. container.completed()
    34. }
    35.  
    36. Component
    37. {
    38. id: delg
    39. Item
    40. {
    41. Rectangle
    42. {
    43. id: moveMe
    44. height: 10; width: 10
    45. border.width: 1; border.color: "black"
    46. state: model.data
    47. color: state
    48.  
    49. MouseArea {
    50. anchors.fill: parent
    51. onClicked: {
    52. moveMe.state = (moveMe.state == "red" ? "blue" : "red")
    53. }
    54. }
    55.  
    56. states: [
    57. State {
    58. name: 'red'
    59. ParentChange { target: moveMe; parent: windowAId; x: windowAId.x }
    60. },
    61. State {
    62. name: 'blue'
    63. ParentChange { target: moveMe; parent: windowBId; x: windowBId.x }
    64. }
    65. ]
    66. }
    67. }
    68. }
    69.  
    70. Repeater {
    71. id: repeat
    72. model: fooModel
    73. delegate: delg
    74. }
    75. }
    To copy to clipboard, switch view to plain text mode 

    It results in this:

    2014-01-27-131146_1280x1024_scrot.png



    How should I refer to WindowA and WindowB from this QML file if they are placed in a different QML file? (without using a loader)

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to move an QML object from one QML file to another, dynamically?

    I am not sure I quite understand your question. WindowA and WindowB are already defined in seperate QML files, no? WindowA.qml and WindowB.qml respectively, right?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to move an QML object from one QML file to another, dynamically?

    Quote Originally Posted by anda_skoa View Post
    I am not sure I quite understand your question. WindowA and WindowB are already defined in seperate QML files, no? WindowA.qml and WindowB.qml respectively, right?
    Thanks for responding.
    Yes, but they are being "used" in file X.qml with their "id" as "windowA" and "windowB".
    How do I make the objects "windowA" and "windowB" (used in X.qml) a parent in this file (main.qml)?

    am I clear now?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to move an QML object from one QML file to another, dynamically?

    I am not sure I understand, but if you instantiate WindowA and WindowB in another compenent but need access to them from outside that component, then this component must provide the access, e.g. something like

    X.qml
    Qt Code:
    1. Item {
    2. property Item windowA: WIndowA {}
    3. property Item windowB: WindowB {}
    4. }
    To copy to clipboard, switch view to plain text mode 

    main.qml
    Qt Code:
    1. Item {
    2. X { id: x }
    3.  
    4. anchors.right: x.windowA.right
    5. }
    To copy to clipboard, switch view to plain text mode 

    The real question will be, however, is this is a good design. A component is a natural encapsulation unit, it shouldn't be necessary to take things out of it.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    TheIndependentAquarius (28th January 2014)

  6. #5
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to move an QML object from one QML file to another, dynamically?

    Quote Originally Posted by anda_skoa View Post
    The real question will be, however, is this is a good design. A component is a natural encapsulation unit, it shouldn't be necessary to take things out of it.
    Thankful for your hint.
    I changed your code a bit and the following worked:

    Qt Code:
    1. property Item windowA: windowAId
    2. property Item windowB: windowBId
    3.  
    4. WindowA
    5. {
    6. x: 0
    7. id: windowAId
    8. color: "green"
    9. width: 70
    10. height: width
    11. }
    12.  
    13. WindowB
    14. {
    15. y: 80
    16. id: windowBId
    17. color: "black"
    18. }
    To copy to clipboard, switch view to plain text mode 

    This is surely NOT a "meaningful" design, but the probelm is that we need to show a part of a video stream in one tab of our QML file and other part of the same stream in another tab.

    We are using a newer version of this: http://gstreamer.freedesktop.org/dat...p-example.html

    This thing "crashes" the whole application if we create two QML instances of it.
    I even tried to use the "loader", but that didn't prevent it from crashing.
    Now moving windows is the last option we have for preventing the application from crashing.

Similar Threads

  1. Replies: 4
    Last Post: 7th November 2012, 03:52
  2. Two dynamically created object interaction issue
    By kornicameister in forum Qt Quick
    Replies: 2
    Last Post: 9th September 2011, 11:35
  3. Replies: 19
    Last Post: 18th October 2009, 09:34
  4. finding dynamically create object
    By abrou in forum Newbie
    Replies: 2
    Last Post: 5th March 2008, 21:17
  5. Move mouse over object
    By ToddAtWSU in forum Qt Programming
    Replies: 17
    Last Post: 3rd October 2007, 15:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.