Results 1 to 6 of 6

Thread: onEntered or onPressed does not work for custom type(QDeclarativeItem subclass)

  1. #1
    Join Date
    Jul 2011
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default onEntered or onPressed does not work for custom type(QDeclarativeItem subclass)

    Hello,

    I have a custom QDeclarativeItem subclass named Polygon. I add a MouseArea in it but onEntered or onPressed does not working, or am i expect wrong thing to happen? I can see my polygons on the window but nothing is writing on console onPressed or onEntered.

    Here is QML file:
    Qt Code:
    1. import MyTypes 1.0
    2. import QtQuick 1.0
    3. import Qt 4.7
    4.  
    5. Item {
    6. id: container
    7. width: 350; height: 250
    8.  
    9. Polygon {
    10. id: aPolygon
    11.  
    12. width: 20; height: 20
    13. name: "A simple polygon"
    14. color: "blue"
    15. vertices:[
    16.  
    17. Point{x:20.0; y:40.0},
    18. Point{x:40.0; y:40.0},
    19. Point{x:40.0; y:20.0},
    20. Point{x:20.0; y:20.0}
    21. ]
    22.  
    23. MouseArea{
    24. anchors.fill: parent
    25. drag.target: aPolygon
    26. drag.axis: Drag.XandYAxis
    27. drag.minimumX: 0
    28. drag.maximumX: container.width - parent.width
    29. drag.minimumY: 0
    30. drag.maximumY: container.height - parent.width
    31. onPressed:console.log("============== ==onPressed")
    32.  
    33. }
    34.  
    35.  
    36. }
    37.  
    38. Polygon {
    39. id: bPolygon
    40. //anchors.centerIn: parent
    41. width: 20; height: 20
    42. name: "A simple polygon"
    43. color: "blue"
    44. vertices:[
    45.  
    46. Point{x:60.0; y:80.0},
    47. Point{x:80.0; y:80.0},
    48. Point{x:80.0; y:60.0},
    49. Point{x:60.0; y:60.0}
    50. ]
    51.  
    52. MouseArea{
    53. //hoverEnabled: false
    54. enabled: visible
    55. hoverEnabled: visible
    56. anchors.fill: parent
    57. acceptedButtons: Qt.LeftButton | Qt.RightButton
    58. onEntered: {
    59. console.log("============== ==onEntered")
    60.  
    61. }
    62. }
    63.  
    64.  
    65. }
    66. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any idea.

  2. #2
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: onEntered or onPressed does not work for custom type(QDeclarativeItem subclass)

    Try to draw a rectangle instead of the MouseArea.
    I think, since you don't set x and y, but set width and height to 20, the MouseArea are in the upper-left corner of the screen. The reason why you see the polygons, that clipping property is set to false by default.

  3. The following user says thank you to laszlo.gosztola for this useful post:

    tuxit (15th August 2011)

  4. #3
    Join Date
    Jul 2011
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: onEntered or onPressed does not work for custom type(QDeclarativeItem subclass)

    Thanks very much, i saw it is the point.
    I can see some event reaction after setting x and y on screen but not in polygons,
    So I want to ask,
    what should i set to x and y?

  5. #4
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: onEntered or onPressed does not work for custom type(QDeclarativeItem subclass)

    Did You draw the rectangles instead of the mouseAreas?

    I think your first polygon now draws a square at point(20,20), point(20, 40), point(40,40), point(40,20).
    But You set the width and height only to 20.

    Maybe your function should look something like this:

    Qt Code:
    1. Polygon {
    2. id: aPolygon
    3. x: 20; y: 20
    4. width: 20; height: 20
    5. name: "A simple polygon"
    6. color: "blue"
    7. vertices:[
    8.  
    9. Point{x:0.0; y:20.0},
    10. Point{x:20.0; y:20.0},
    11. Point{x:20.0; y:0.0},
    12. Point{x:0.0; y:0.0}
    13. ]
    14.  
    15. MouseArea{
    16. anchors.fill: parent
    17. drag.target: aPolygon
    18. drag.axis: Drag.XandYAxis
    19. drag.minimumX: 0
    20. drag.maximumX: container.width - parent.width
    21. drag.minimumY: 0
    22. drag.maximumY: container.height - parent.width
    23. onPressed:console.log("============== ==onPressed")
    24.  
    25. }
    26.  
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jul 2011
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: onEntered or onPressed does not work for custom type(QDeclarativeItem subclass)

    unfortunately can not working right.
    thanks for attention


    Added after 1 37 minutes:


    i tried to draw a triangle with Polygon{} but MouseArea still cares the rectangle that is defined by x, y, width and height.
    the shape function does not matter i think.
    Last edited by tuxit; 15th August 2011 at 18:03.

  7. #6
    Join Date
    Dec 2010
    Location
    Veszprém, Hungary
    Posts
    34
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: onEntered or onPressed does not work for custom type(QDeclarativeItem subclass)

    Of course. How should it know that you draw a polygon??
    You just filled the rectangle with the MouseArea. If you want it, you have to get the x and y coordinates of the click, check if it's in the polygon decide if you want to react or not.

Similar Threads

  1. Using a QML model from a QDeclarativeItem subclass
    By humbleguru in forum Qt Programming
    Replies: 1
    Last Post: 3rd January 2011, 12:07
  2. QGraphicsItem subclass and accessing custom properties
    By been_1990 in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2010, 01:48
  3. Replies: 3
    Last Post: 10th December 2009, 22:53
  4. Replies: 3
    Last Post: 26th November 2007, 18:01
  5. Posting custom events to a subclass of QThread
    By jpn in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2006, 15:49

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.