Results 1 to 2 of 2

Thread: setting a custom, visual property

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default setting a custom, visual property

    Hello,

    Just for fun I set out to make a super-duper, absurdly simple Chess ui in pure QML. There is no computer opponent, not even move validation, but two human players can move pieces around the board -- if no one tries to cheat, it works great. I came across a bit of a puzzler while doing it though.

    Consider the following:

    Qt Code:
    1. /* Chessman.qml */
    2.  
    3. Item {
    4. id: root
    5.  
    6. property string symbol
    7. property color faction
    8.  
    9. anchors.centerIn: parent
    10.  
    11. Text {
    12. text: symbol
    13. color: faction
    14. anchors.centerIn: parent
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. /* Square.qml */
    2.  
    3. Rectangle {
    4. id: root
    5.  
    6. signal clicked()
    7.  
    8. property Chessman occupant
    9.  
    10. MouseArea {
    11. anchors.fill: parent
    12. onClicked: {
    13. console.debug(occupant ? occupant.symbol : "No occupant!")
    14. root.clicked()
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    Now, what I tried to do for a given square of the board was this (here, the starting, A8 Rook):

    Qt Code:
    1. Square {
    2. id: a8
    3. width: board.squareW
    4. height: board.squareH
    5. color: "white"
    6. occupant: Chessman {
    7. symbol: "R"
    8. faction: "red"
    9. }
    10. onClicked: { ... }
    11. }
    To copy to clipboard, switch view to plain text mode 

    But I could not see my Chessman. I went Googling and found something in the Qt docs that concerned parenting VS properties, and indeed, if I insert "parent: a8" into a8's Chessman code, then voila.

    So, my questions are:
    1) Is there a better way to do this? Not on a grand scale, I mean to do this particular intended thing. For example, please don't tell me that perhaps the set of squares should populate a model and be shown in a GridView.
    2) The real question: given my experience with Chessman, why does the gradient property of Rectangle work the way it does? I thought I was patterning my Chessman property in Square accordingly, but you can SEE the gradient in the parent rectangle after setting it in the same way. Is there something magical going on? Any insight appreciated.

  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: setting a custom, visual property

    Quote Originally Posted by Urthas View Post
    1) Is there a better way to do this? Not on a grand scale, I mean to do this particular intended thing.
    There are many possible solutions. One would be to use onOccupantChanged signal and set the parent of the piece there. However personally I don't think the piece should be a child of the square. They should rather be siblings, at least in my opinion.

    2) The real question: given my experience with Chessman, why does the gradient property of Rectangle work the way it does? I thought I was patterning my Chessman property in Square accordingly, but you can SEE the gradient in the parent rectangle after setting it in the same way. Is there something magical going on? Any insight appreciated.
    I cannot answer that question without seeing what you get. You didn't post any code related to this question.
    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. Custom Property On Custom Widget
    By Ashutosh2k1 in forum Qt Programming
    Replies: 29
    Last Post: 28th October 2014, 12:16
  2. Custom property in designer
    By avgust in forum Newbie
    Replies: 1
    Last Post: 9th September 2010, 10:14
  3. Static plugin: setting/reading a property
    By eg3gg in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2010, 17:12
  4. Controlling Property Setting Order
    By thePoet in forum Qt Tools
    Replies: 1
    Last Post: 13th July 2010, 19:29
  5. Replies: 2
    Last Post: 27th May 2010, 01:55

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.