Results 1 to 2 of 2

Thread: Unable to access custom property

  1. #1
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Unable to access custom property

    Hi everyone!
    I'm experimenting with QtQuick and started with 1.0 because that's what my distro ships by default.
    I made a couple of custom objects and am trying to create some reusable object with them, but I can't access one of the custom properties.
    Would someone please point me to the right direction on this? Thank you!

    -- Information and code below --

    The errors I get are:
    Qt Code:
    1. app.qml:17:18: Cannot assign to non-existent property "name"
    2. //or
    3. app.qml:17:13: Invalid grouped property access
    To copy to clipboard, switch view to plain text mode 

    Here's my directory structure:

    Qt Code:
    1. app/
    2. ├── Components
    3. │** ├── Button.qml
    4. │** ├── fa-glyphs.js
    5. │** └── FAicon.qml
    6. ├── app.kdev4
    7. ├── app.qml
    8. └── Resources
    9. └── fonts
    10. └── fontawesome.ttf
    To copy to clipboard, switch view to plain text mode 

    Here's the scripts:

    FAicon.qml
    Qt Code:
    1. import QtQuick 1.0
    2. import "fa-glyphs.js" as FontAwesome
    3.  
    4. Item {
    5. property alias color: textContent.color
    6. property alias size: textContent.font.pointSize
    7. property variant iconSet: FontAwesome.Icons
    8. property variant name: "none"
    9. FontLoader {
    10. source: "../Resources/fonts/fontawesome.ttf"
    11. }
    12.  
    13. Text {
    14. id: textContent
    15. font.family: "FontAwesome"
    16. text: parent.iconSet[parent.name]
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    Button.qml
    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. Rectangle {
    4. property alias text: buttonText.text
    5. property alias icon: icon // Also tried with property variant icon: icon, but got the second error.
    6.  
    7. FAicon {
    8. id: icon
    9. name: "none"
    10. }
    11.  
    12. Text {
    13. id: buttonText
    14. anchors.centerIn: parent
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    And app.qml
    Qt Code:
    1. import QtQuick 1.0
    2. import "Components"
    3.  
    4. Rectangle {
    5. id: mainwindow
    6. width: 800
    7. height: 500
    8. color: "#cecece"
    9.  
    10. Rectangle {
    11. id: sidebar
    12. width: parent.width * 0.2
    13. height: parent.height
    14. color: "#272727"
    15.  
    16. Button {
    17. icon.name: "envelope" // This line throws errors
    18. text: "New message!"
    19. anchors.horizontalCenter: parent.horizontalCenter
    20. }
    21. }
    22.  
    23. Rectangle {
    24. id: messageView
    25. width: parent.width * 0.3
    26. height: parent.height
    27. color: "darkred"
    28. anchors.left: sidebar.right
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Unable to access custom property

    Entities defined within an element are not visible outside an element. You cannot access "icon" or "buttonText" from outside of a Button instance.

    You can do this though:

    javascript Code:
    1. import QtQuick 1.0
    2.  
    3. Rectangle {
    4. property alias text: buttonText.text
    5. property alias icon: icon.name
    6.  
    7. FAicon {
    8. id: icon
    9. name: "none"
    10. }
    11.  
    12. Text {
    13. id: buttonText
    14. anchors.centerIn: parent
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    And then use it like you did with button text:

    javascript Code:
    1. Button {
    2. icon: "envelope"
    3. text: "New message!"
    4. anchors.horizontalCenter: parent.horizontalCenter
    5. }
    To copy to clipboard, switch view to plain text mode 

    Alternatively you can let the user provide a component for an icon:

    javascript Code:
    1. import QtQuick 1.0
    2.  
    3. Rectangle {
    4. property alias text: buttonText.text
    5. property alias icon: icon // Also tried with property variant icon: icon, but got the second error.
    6.  
    7. property Component icon
    8.  
    9. Loader {
    10. component: icon
    11. }
    12.  
    13. Text {
    14. id: buttonText
    15. anchors.centerIn: parent
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    And then:

    javascript Code:
    1. Button {
    2. icon: FAicon { ... }
    3. text: "New message!"
    4. anchors.horizontalCenter: parent.horizontalCenter
    5. }
    To copy to clipboard, switch view to plain text mode 

    BTW. I really advise you use Qt5 and QtQuick 2.x instead of QtQuick 1. It has much more capabilities.
    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. The following user says thank you to wysota for this useful post:

    Axtroz (6th March 2015)

Similar Threads

  1. how to access QAxObject property?
    By 0BLACK0 in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2015, 11:53
  2. Access parent property from repeater
    By c1223 in forum Qt Quick
    Replies: 3
    Last Post: 28th November 2014, 06:11
  3. Access object property in JS
    By folibis in forum Qt Quick
    Replies: 1
    Last Post: 18th January 2014, 13:23
  4. Replies: 2
    Last Post: 27th October 2013, 18:23
  5. Replies: 1
    Last Post: 20th February 2012, 20:44

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
  •  
Qt is a trademark of The Qt Company.