Results 1 to 10 of 10

Thread: Cant access alias from Item

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Cant access alias from Item

    Hello!

    I am trying to access an alias from an Item and it is not working. Can anyone help me?

    In main.qml, when I try DelegateLancamentos.removeMouseArea: it returns this message:
    qrc:/DelegateLancamentos.qml:10 Invalid alias reference. Unable to find id "removeMouseArea"

    Code below:

    MainForm.ui.qml
    Qt Code:
    1. Rectangle {
    2. id: mainContainer
    3.  
    4. property alias lancamentosListView: lancamentosListView
    5.  
    6. width: 360
    7. height: 640
    8.  
    9. ListView {
    10. id: lancamentosListView
    11.  
    12. anchors.fill: parent
    13.  
    14. clip: true
    15.  
    16. spacing: 10
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    ModeloLancamentos.qml
    Qt Code:
    1. ListModel {
    2. ListElement {
    3. valor: "25.000,00"
    4. tipo: "Investimento"
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    DelegateLancamentos.qml
    Qt Code:
    1. Item {
    2. id: delegateItem
    3.  
    4. property Component delegateComponent: delegateComponent
    5. property alias removeMouseArea: removeMouseArea
    6.  
    7. Component {
    8. id: delegateComponent
    9.  
    10. Row {
    11. Column {
    12. width: 360 * 0.96 / 8 * 6
    13. //width: Screen.width * 0.96 / 8 * 6
    14.  
    15. Text {
    16. text: "<strong>" + "Valor: " + valor + "</strong>"
    17. font.pixelSize: 16
    18. }
    19. Text {
    20. text: "Tipo: " + tipo
    21. font.pixelSize: 16
    22. }
    23. }
    24.  
    25. Column {
    26. width: 360 * 0.96 / 8
    27. //width: Screen.width * 0.96 / 8
    28.  
    29. Image {
    30. id: removeImage
    31. source: "Imagens/remove.png"
    32.  
    33. width: 35
    34.  
    35. fillMode: Image.PreserveAspectFit
    36.  
    37. MouseArea {
    38. id: removeMouseArea
    39.  
    40. anchors.fill: parent
    41. }
    42. }
    43. }
    44. }
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 

    main.qml
    Qt Code:
    1. Window {
    2. visible: true
    3.  
    4. width: 360
    5. height: 640
    6.  
    7. maximumHeight: 640
    8. minimumHeight: 640
    9.  
    10. maximumWidth: 360
    11. minimumWidth: 360
    12.  
    13. title: "InvestmentC-Mobile"
    14.  
    15. MainForm {
    16. anchors.fill: parent
    17.  
    18. mainContainer.width: parent.width
    19. mainContainer.height: parent.height
    20.  
    21. lancamentosListView.model: modeloLancamentos
    22. lancamentosListView.delegate: delegateLancamentos.delegateComponent
    23.  
    24. DelegateLancamentos.removeMouseArea:
    25. {
    26. modeloLancamentos.remove(index);
    27. }
    28. }
    29.  
    30. ModeloLancamentos{
    31. id: modeloLancamentos
    32. }
    33.  
    34. DelegateLancamentos {
    35. id: delegateLancamentos
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

  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: Cant access alias from Item

    Line 24 of main.qml doesn't seem to be valid. What is it supposed to do?
    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
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cant access alias from Item

    Hello wysota!

    Thanks for the reply

    What I really need is that:

    In MainForm.ui.qml I have two rectangle. I need when i click the mouse area from a delegate component set rec1.visible = false and rec2.visible = true (files below)

    How can I do that?

    Qt Code:
    1. MainForm.ui.qml
    2.  
    3. Rectangle {
    4. id: mainContainer
    5.  
    6. property alias lancamentosListView: lancamentosListView
    7. property alias rc1: rec1
    8. property alias rc2: rec2
    9.  
    10. width: 360
    11. height: 640
    12.  
    13. ListView {
    14. id: lancamentosListView
    15.  
    16. anchors.fill: parent
    17.  
    18. clip: true
    19.  
    20. spacing: 10
    21. }
    22.  
    23. Rectangle {
    24. id: rec1
    25. }
    26.  
    27. Rectangle {
    28. id: rec2
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ModeloLancamentos.qml
    2.  
    3. ListModel {
    4. ListElement {
    5. valor: "25.000,00"
    6. tipo: "Investimento"
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. DelegateLancamentos.qml
    2.  
    3. Item {
    4. id: delegateItem
    5.  
    6. property Component delegateComponent: delegateComponent
    7. property alias removeMouseArea: removeMouseArea
    8.  
    9. Component {
    10. id: delegateComponent
    11.  
    12. Row {
    13. Column {
    14. width: 360 * 0.96 / 8 * 6
    15. //width: Screen.width * 0.96 / 8 * 6
    16.  
    17. Text {
    18. text: "<strong>" + "Valor: " + valor + "</strong>"
    19. font.pixelSize: 16
    20. }
    21. Text {
    22. text: "Tipo: " + tipo
    23. font.pixelSize: 16
    24. }
    25. }
    26.  
    27. Column {
    28. width: 360 * 0.96 / 8
    29. //width: Screen.width * 0.96 / 8
    30.  
    31. Image {
    32. id: removeImage
    33. source: "Imagens/remove.png"
    34.  
    35. width: 35
    36.  
    37. fillMode: Image.PreserveAspectFit
    38.  
    39. MouseArea {
    40. id: removeMouseArea
    41.  
    42. anchors.fill: parent
    43. }
    44. }
    45. }
    46. }
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. main.qml
    2.  
    3. Window {
    4. visible: true
    5.  
    6. width: 360
    7. height: 640
    8.  
    9. maximumHeight: 640
    10. minimumHeight: 640
    11.  
    12. maximumWidth: 360
    13. minimumWidth: 360
    14.  
    15. title: "InvestmentC-Mobile"
    16.  
    17. MainForm {
    18. anchors.fill: parent
    19.  
    20. mainContainer.width: parent.width
    21. mainContainer.height: parent.height
    22.  
    23. lancamentosListView.model: modeloLancamentos
    24. lancamentosListView.delegate: delegateLancamentos.delegateComponent
    25.  
    26. DelegateLancamentos.removeMouseArea:
    27. {
    28. modeloLancamentos.remove(index);
    29. }
    30. }
    31.  
    32. ModeloLancamentos{
    33. id: modeloLancamentos
    34. }
    35.  
    36. DelegateLancamentos {
    37. id: delegateLancamentos
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

  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: Cant access alias from Item

    Declare a signal in DelegateLancamentos and have it emitted when the area is clicked. Then provide a handler for that signal wherever you use that element. If you need, you can expose a property in main element of MainForm.ui.qml that tells which of the two rectangles should be visible so that it is easier to perform the task you want from within main.qml.

    I have a feeling that your thinking is too much imperative. You are working within a declarative environment so try making your code more declarative. E.g. don't think what happens if you click on the mouse area but rather think when should each of the rectangles be visible.

    Consider the following imperative code:

    javascript Code:
    1. Item {
    2. Rectangle { id: r1: visible: true }
    3. Rectangle { id: r2: visible: false }
    4.  
    5. MouseArea {
    6. anchors.fill: parent
    7. onClicked: { r1.visible = false; r2.visible = true }
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    And its possible declarative counterpart:
    javascript Code:
    1. Item {
    2. property bool r2Visible: false
    3.  
    4. Rectangle { id: r1: visible: !r2.visible /* or !r2Visible */ }
    5. Rectangle { id: r2: visible: r2Visible }
    6.  
    7. MouseArea {
    8. anchors.fill: parent
    9. onClicked: { r2Visible = true }
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Alternatively a state machine can be used to achieve a similar result without "r2Visible" 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.


  5. #5
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cant access alias from Item

    Hey wysota

    But where I declare the property. I tried to declare inside MainForm.ui.qml, inside MainForm at main.qml and inside DelegateLancamentos.qml. In neither of them I could access the property from the MouseArea inside the delegate component.

  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: Cant access alias from Item

    Quote Originally Posted by guidupas View Post
    In neither of them I could access the property from the MouseArea inside the delegate component.
    Look at my second code snippet. Line #2 contains the property and line #9 contains a reference to it. Based on that you can see the property should be placed in delegateItem.
    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.


  7. #7
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cant access alias from Item

    wysota, thank you very much for the help. It worked.

    I am posting here a minimal example just to exemplify for who needs.

    MainForm.ui.qml
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.2
    3. import QtQuick.Layouts 1.1
    4.  
    5. Rectangle {
    6. id: mainContainer
    7.  
    8. property alias mainContainerA: mainContainer
    9. property alias buttonA: button
    10. property alias listviewA: listView
    11. property alias rect1A: rect1
    12. property alias rect2A: rect2
    13.  
    14. width: 600
    15. height: 600
    16.  
    17. Button {
    18. id: button
    19.  
    20. anchors.top: parent.top
    21.  
    22. text: "Insert value"
    23.  
    24. height: parent.height * 0.1
    25. width: parent.width
    26. }
    27.  
    28. ListView {
    29. id: listView
    30.  
    31. anchors.top: button.bottom
    32.  
    33. clip: true
    34.  
    35. height: parent.height * 0.5
    36. width: parent.width
    37. }
    38.  
    39. Rectangle {
    40. id: rect1
    41.  
    42. visible: true
    43.  
    44. anchors.top: listView.bottom
    45.  
    46. width: parent.width
    47. height: parent.height * 0.4
    48.  
    49. color: "blue"
    50. }
    51.  
    52. Rectangle {
    53. id: rect2
    54.  
    55. visible: false
    56.  
    57. anchors.top: listView.bottom
    58.  
    59. width: parent.width
    60. height: parent.height * 0.4
    61.  
    62. color: "red"
    63. }
    64. }
    To copy to clipboard, switch view to plain text mode 

    main.qml
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Window 2.2
    3.  
    4. Window {
    5. visible: true
    6.  
    7. width: 600
    8. height: 600
    9.  
    10. MainForm {
    11. anchors.fill: parent
    12.  
    13. listviewA.model: modelM
    14. listviewA.delegate: delegateM.delegateComponent
    15.  
    16. buttonA.onClicked: {
    17. modelM.append({valor: 100, tipo: 2, tipoIndex: 3});
    18. }
    19.  
    20. rect1A.visible: delegateM.rect1Visible
    21. rect2A.visible: delegateM.rect2Visible
    22. }
    23.  
    24. Delegate {
    25. id: delegateM
    26. }
    27.  
    28. Model {
    29. id: modelM
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    Delegate.qml
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.2
    3. import QtQuick.Window 2.2
    4.  
    5. Item {
    6. id: delegateItem
    7.  
    8. property Component delegateComponent: delegateComponent
    9. property bool rect1Visible: true
    10. property bool rect2Visible: false
    11.  
    12. Component {
    13. id: delegateComponent
    14.  
    15. Row {
    16. height: 53
    17.  
    18. Column {
    19. width: 600 * 0.96 / 8 * 6
    20. //width: Screen.width * 0.96 / 8 * 6
    21.  
    22.  
    23. Text {
    24. text: "<strong>" + "Valor: " + valor + "</strong>"
    25. font.pixelSize: 16
    26. }
    27. Text {
    28. text: "Tipo: " + tipo
    29. font.pixelSize: 16
    30. }
    31. }
    32.  
    33. Column {
    34. width: 600 * 0.96 / 8
    35. //width: Screen.width * 0.96 / 8
    36.  
    37. Rectangle {
    38. color: "grey"
    39. width: 50
    40. height: 50
    41. MouseArea {
    42. id: editMouseArea
    43.  
    44. anchors.fill: parent
    45.  
    46. onClicked:
    47. {
    48. rect1Visible = true;
    49. rect2Visible = false;
    50. }
    51. }
    52. }
    53.  
    54. }
    55.  
    56. Column {
    57. width: 600 * 0.96 / 8
    58. //width: Screen.width * 0.96 / 8
    59.  
    60. Rectangle {
    61. color: "darkred"
    62. width: 50
    63. height: 50
    64. MouseArea {
    65. id: removeMouseArea
    66.  
    67. anchors.fill: parent
    68.  
    69. onClicked:
    70. {
    71. rect1Visible = false;
    72. rect2Visible = true;
    73. }
    74. }
    75. }
    76. }
    77. }
    78. }
    79. }
    To copy to clipboard, switch view to plain text mode 

    Model.qml
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. ListModel {
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: Cant access alias from Item

    Sorry to criticise your code but you should really avoid exposing all those items as aliases.
    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.


  9. #9
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Cant access alias from Item

    No problem. I am new in QML and thats is the best way to learn more. Your criticals and suggestions are welcome.

  10. #10
    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: Cant access alias from Item

    Exposing those items means you want to be able to access any property of each of them from anywhere. In that case you should probably either put all the code in one document or decide which set of properties to expose from each object and alias just that. Alternatively use components to decide about an internal item from outside of its parent item, just like views do with delegates.

    Something along the lines of:

    javascript Code:
    1. Item {
    2. id: root
    3.  
    4. property Component rect1Component: Rectangle { /* default rec1 */ ... }
    5.  
    6. Loader {
    7. id: rect1
    8. sourceComponent: rect1Component
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Then you can program that item from a document which instantiates it by assigning a different component to rect1Component.
    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. Replies: 16
    Last Post: 12th October 2015, 16:02
  2. Replies: 1
    Last Post: 9th May 2013, 15:53
  3. Replies: 2
    Last Post: 3rd February 2011, 10:07
  4. Replies: 2
    Last Post: 24th May 2009, 10:27
  5. qresource file alias problem with svg
    By giotto in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2008, 15:59

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.