Results 1 to 6 of 6

Thread: Change state on dynamic elements.

  1. #1
    Join Date
    Aug 2013
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Change state on dynamic elements.

    Hello,
    In my program I add some buttons dynamically and I try to switch between two mode. The first mode is to move the buttons and the second to make it clickable and non-movable.
    My problem is I do not know how to change the state of an object dynamically added.

    Here my source code:

    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Controls 1.0
    3.  
    4. Rectangle {
    5. id: page
    6. width: 360
    7. height: 360
    8. Text {
    9. text: qsTr("Hello World")
    10. anchors.centerIn: parent
    11. }
    12.  
    13. Row {
    14. id: myRow
    15. anchors.fill: parent
    16. }
    17.  
    18. Component {
    19. id: myRectComp
    20.  
    21. Item {
    22. id: cell
    23. x: 0
    24. y: 30
    25.  
    26. Button {
    27. y: 30
    28. text: qsTr("My Butt")
    29.  
    30. MouseArea {
    31. id: contentMouseArea
    32. anchors.fill: parent
    33. drag.target: cell
    34.  
    35. // States to make an element in edition mode (movable) or not (clickable)
    36. states: [
    37. State {
    38. name: "movable"
    39. PropertyChanges { target: myRow; anchors.fill: parent }
    40. },
    41.  
    42. State {
    43. name: "clickable"
    44. PropertyChanges { target: myRow; anchors.fill: undefined }
    45. }
    46. ]
    47. }
    48. }
    49. }
    50. }
    51.  
    52. // Toolbar
    53. Row {
    54. id: toolbar
    55. anchors.fill: parent
    56.  
    57. Button {
    58. x: 0
    59. y: 0
    60. text: qsTr("Add Button")
    61. onClicked: {
    62. var rect = myRectComp.createObject(myRow)
    63. }
    64. }
    65.  
    66. Button {
    67. text: qsTr("Edition")
    68. checkable: true
    69. checked: true
    70. onClicked: contentMouseArea.state= 'clickable'
    71. }
    72. }
    73. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for your help.

    EDIT: I found that on http://qt-project.org/doc/qt-4.7/qde...-59d951346a29:
    Also, note that while dynamically created objects may be used the same as other objects, they do not have an id in QML.
    So how can I do?
    Last edited by Binpix; 8th September 2013 at 22:40.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Change state on dynamic elements.

    When creating an object, assign it to some variable (like "rect" in your code but something you can access from outside the function creating the object). You can then access the object through the variable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2013
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Change state on dynamic elements.

    Thank you. Do you have an example?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Change state on dynamic elements.

    Here is one option:

    javascript Code:
    1. Item {
    2. property Item dynamicItem
    3.  
    4. function createItem() { dynamicItem = cmpnt.createObject(...); }
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2013
    Posts
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Change state on dynamic elements.

    Thank you, but in my case I cannot figure out how to do to change the state of my element. Can you help me?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Change state on dynamic elements.

    Assign the state name to the "state" property.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Change state property within a current state?
    By prophet0 in forum Qt Quick
    Replies: 1
    Last Post: 30th May 2012, 20:23
  2. Replies: 3
    Last Post: 26th July 2011, 20:11
  3. Replies: 1
    Last Post: 20th January 2011, 18:17
  4. Handling window state change
    By sudhansu in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2010, 13:28
  5. Change position according to QCheckBox State
    By champ in forum Qt Programming
    Replies: 5
    Last Post: 6th June 2010, 19:37

Tags for this Thread

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.