Results 1 to 1 of 1

Thread: Positioning problem, anchors not allowed

  1. #1
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Positioning problem, anchors not allowed

    I am writing arkanoid-like game for a friend, and I have stuck on positioning two dynamically placed items.

    I prepared my own grid like this
    Qt Code:
    1. Item {
    2. id: abstractGrid //this is the item which goes from top of the screen right to top of the toolBar
    3. width: screen.width
    4. anchors {
    5. top: screen.top; bottom: toolBar.top
    6. }
    7.  
    8. Image {
    9. id: background
    10. source: "resources/background.jpg"
    11. anchors.fill: abstractGrid
    12. fillMode: Image.PreserveAspectCrop
    13.  
    14. Item {
    15. id: grid
    16. anchors.fill: background
    17.  
    18. Item {
    19. id: barGrid
    20. width: grid.width-10; height: 20
    21. anchors {bottom: grid.bottom ; centerIn: grid.Center}
    22. }
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    where I am gonna place all stones to be hit along with my ball and bar.
    Here is the code I use to create them (without stones at the moment)
    Qt Code:
    1. function createBar(){
    2. console.log("creating bar")
    3. if(barComponent == null){
    4. barComponent = Qt.createComponent("Bar.qml");
    5. }
    6. if(barComponent.status == Component.Ready){
    7. var dynamicBar = barComponent.createObject(barGrid)
    8. if(dynamicBar == null){
    9. console.log("error creating bar " + barComponent.errorString()); return false;
    10. }
    11. dynamicBar.width = 50
    12. dynamicBar.height = barGrid.height
    13. dynamicBar.x = barGrid.width / 2
    14. items[0] = dynamicBar;
    15. createBall();
    16. }else{
    17. console.log("error loading block barComponent" + barComponent.errorString()); return false;
    18. }
    19. return true;
    20. }
    21.  
    22. function createBall(){
    23. console.log("creating ball")
    24. if(balComponent == null){
    25. balComponent = Qt.createComponent("Bal.qml");
    26. }
    27. if(balComponent.status == Component.Ready){
    28. var dynamicBal = balComponent.createObject(grid)
    29. if(dynamicBal == null){
    30. console.log("error creating ball " + balComponent.errorString()); return false;
    31. }
    32. dynamicBal.type = Math.floor(Math.random() * 3)
    33. dynamicBal.y = grid.height - barGrid.height - toolBar.height - 6
    34. dynamicBal.x = barGrid.width / 2
    35. dynamicBal.onBar = true
    36. dynamicBal.height = 25
    37. dynamicBal.width = 25
    38. items[1] = dynamicBal
    39. }else{
    40. console.log("error loading block balComponent" + balComponent.errorString()); return false;
    41. }
    42. return true;
    43. }
    To copy to clipboard, switch view to plain text mode 

    the are created without any error, but their relative positions are incorrect and look like this

    but I want them to be positioned one above another directly.
    the problem of course lies in anchoring, because the x-coordinate of tha bar and ball seems to be placed in their left side and not in the center.
    To be precise, I am not sure whether or not I should use anchors here to place x-coordinate in the center of the object but I know that if I use any anchors.xx code it will prevent my items from moving which I require.
    So how can I make x-coordinate to be placed in the center of the item ??

    I attach code for bar and ball as well
    Qt Code:
    1. import QtQuick 1.0
    2.  
    3. ///there will be three possible bal (red,green,blue)
    4.  
    5. Item {
    6. id: bal
    7. property int type: 0
    8. property bool nitro: false //ball goes fast
    9. property bool destroyer: false //ball do not bounce from obstacles it goes throught them crushig em
    10. property bool onBar: false
    11.  
    12. //setting the bal colour
    13. Image {
    14. id: bal_colour
    15. anchors.fill: bal
    16. smooth: true
    17. source: {
    18. if(type == 0){
    19. return "resources/redBall.png";
    20. }else if (type == 1){
    21. return "resources/yellowBall.png";
    22. }else{
    23. return "resources/greenBall.png"
    24. }
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. import QtQuick 1.0
    2. import "logic/barMechanics.js" as BAR_MECHANIC
    3.  
    4. Rectangle {
    5. id: bar
    6. smooth: true
    7. radius: 30
    8. opacity: 0.9
    9. gradient: Gradient {
    10. GradientStop { position: 0.0 ; color: "red"}
    11. GradientStop { position: 0.5 ; color: "yellow" }
    12. GradientStop { position: 1.0 ; color: "black"}
    13. }
    14.  
    15. signal clicked;
    16. property real startX : 0
    17. property real startY : 0
    18.  
    19. MouseArea { //!!!! <--- CUSTOMIZE IT ---> !!!!//
    20. id: mouseArea
    21. anchors.fill: bar
    22. drag.target: bar
    23. drag.axis: Drag.XAxis
    24. drag.minimumX: 0
    25. drag.maximumX: bar.parent.width - bar.width
    26.  
    27. onClicked: BAR_MECHANIC.launchTheBall();
    28. }
    29.  
    30. //add animation movent - the blur effect and starts effect when launching the ball
    31. }
    To copy to clipboard, switch view to plain text mode 


    Added after 1 19 minutes:


    I found the solution for that
    if you want to use anchors and than still have possibility to drag your object, you set anchors values to undefined in onPressed event
    Note: Items cannot be dragged if they are anchored for the requested drag.axis. For example, if anchors.left or anchors.right was set for rect in the above example, it cannot be dragged along the X-axis. This can be avoided by settng the anchor value to undefined in an onPressed handler.
    Last edited by kornicameister; 26th August 2011 at 09:35.
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

Similar Threads

  1. Problem with positioning a QGraphicsItem in the scene
    By qlands in forum Qt Programming
    Replies: 0
    Last Post: 1st October 2010, 11:05
  2. Layout positioning problem.
    By Tomasz in forum Newbie
    Replies: 1
    Last Post: 14th September 2010, 09:42
  3. QGraphicsRectItem positioning problem
    By gufeatza in forum Qt Programming
    Replies: 3
    Last Post: 5th September 2009, 00:24
  4. QWidget positioning problem
    By St@n in forum Qt Programming
    Replies: 7
    Last Post: 30th June 2009, 06:56
  5. problem in positioning item in QGraphicsScene()
    By wagmare in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2009, 06:41

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.