Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. check Hover in pool of rectangles
QtWS25 Last Chance

check Hover in pool of rectangles

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qml canvashoverqt quickpopoveepopup
11 Posts 2 Posters 6.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anas A. Ismail
    wrote on last edited by
    #1

    Hello Everyone,
    I have main Canvas that contains a pool of rectangles I want to show a popup for each hovered rectangle, how could that possibly be done ?
    I always can check this with the last created one, but the taken action applied on all of other rectangles.

    p3c0P 1 Reply Last reply
    0
    • A Anas A. Ismail

      Hello Everyone,
      I have main Canvas that contains a pool of rectangles I want to show a popup for each hovered rectangle, how could that possibly be done ?
      I always can check this with the last created one, but the taken action applied on all of other rectangles.

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Anas-A.-Ismail You can add MouseArea inside each Rectangle and set hoverEnabled property to true the when mouse is hovered over these rectangle onEntered event handler is triggerred. You can invoke a popup from there.

      157

      A 1 Reply Last reply
      0
      • p3c0P p3c0

        @Anas-A.-Ismail You can add MouseArea inside each Rectangle and set hoverEnabled property to true the when mouse is hovered over these rectangle onEntered event handler is triggerred. You can invoke a popup from there.

        A Offline
        A Offline
        Anas A. Ismail
        wrote on last edited by
        #3

        @p3c0 I'm drawing the rectangles dynamically at runtime, so when I hover on one of them, the action accepted from the last created rectangle.

        p3c0P 1 Reply Last reply
        0
        • A Anas A. Ismail

          @p3c0 I'm drawing the rectangles dynamically at runtime, so when I hover on one of them, the action accepted from the last created rectangle.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @Anas-A.-Ismail Do you mean you are drawing rectangles with Canvas API's ?

          157

          A 1 Reply Last reply
          0
          • p3c0P p3c0

            @Anas-A.-Ismail Do you mean you are drawing rectangles with Canvas API's ?

            A Offline
            A Offline
            Anas A. Ismail
            wrote on last edited by
            #5

            @p3c0 I'm drawing rectangles using Mouse (Drag/Drop) on a Canvas, So I have multiple rectangles on the same Canvas which created dynamically on run time using createObject method, I need to deal with each rectangle separately, like this image here

            p3c0P 1 Reply Last reply
            0
            • A Anas A. Ismail

              @p3c0 I'm drawing rectangles using Mouse (Drag/Drop) on a Canvas, So I have multiple rectangles on the same Canvas which created dynamically on run time using createObject method, I need to deal with each rectangle separately, like this image here

              p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              @Anas-A.-Ismail Trying to understand. Did you create Rectangle's using createObject ? In that case you can put MouseArea inside it as said earlier.

              157

              A 1 Reply Last reply
              0
              • p3c0P p3c0

                @Anas-A.-Ismail Trying to understand. Did you create Rectangle's using createObject ? In that case you can put MouseArea inside it as said earlier.

                A Offline
                A Offline
                Anas A. Ismail
                wrote on last edited by
                #7

                @p3c0 Yes I did that, but hover property applied only on the last created Rectangle, and when I hover on it the wanted action applied on all rectangles like this image, I want to apply this action on each rectangle separately without affecting the others.

                Thanks.

                p3c0P 1 Reply Last reply
                0
                • A Anas A. Ismail

                  @p3c0 Yes I did that, but hover property applied only on the last created Rectangle, and when I hover on it the wanted action applied on all rectangles like this image, I want to apply this action on each rectangle separately without affecting the others.

                  Thanks.

                  p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #8

                  @Anas-A.-Ismail That's strange. Each Rectangle will have its own MouseArea and which will generate hover events for that particular Item only. Can you post the relevant code ?

                  157

                  A 1 Reply Last reply
                  0
                  • p3c0P p3c0

                    @Anas-A.-Ismail That's strange. Each Rectangle will have its own MouseArea and which will generate hover events for that particular Item only. Can you post the relevant code ?

                    A Offline
                    A Offline
                    Anas A. Ismail
                    wrote on last edited by p3c0
                    #9

                    @p3c0
                    img.qml

                    Item{
                       Canvas{
                            id: mainCanvase
                            width: 640
                            height: 480
                            visible: true
                            anchors.fill: parent
                    //        layer.enabled: false
                            BoundingBox{
                                id: imgBox
                            }
                    
                            MouseArea{
                                property Rectangle boundingBox: null
                                property Component commentBox: null
                                id: mainCanvaseMouseArea
                                anchors.fill: parent
                    
                                onPressed: {
                                   var boundingBoxObj = imgBox.createObject (mainCanvase, {"x" :mouse.x, "y": mouse.y});
                                    boundingBox = boundingBoxObj;
                                }
                    
                                onReleased: {
                                    if((boundingBox.width >2) && (boundingBox.height > 2))
                                    {
                                        boundingBox.createCommentBox();
                                        mainCanvaseMouseArea.enabled = false;
                                    }
                                    else
                                    {
                                        boundingBox.destroy();
                                    }
                                }
                    
                                onPositionChanged: {
                                    boundingBox.width = (Math.abs (mouse.x - boundingBox.x));
                                    boundingBox.height = (Math.abs (mouse.y - boundingBox.y));
                                }
                            }
                        }
                    }
                    

                    boundingbox.qml

                    Component{
                        id: boundingBoxComponent
                    
                        Rectangle{
                            id: boundingBoxRect
                            objectName: "boundingBoxRect"
                            border.color: "red"
                            radius: 10
                            color: "transparent"
                            property color onHoverColor: "blue"
                            property color defaultColor: "red"
                            property bool isHovered: true
                            property Component commentBox: null
                    
                            function createCommentBox(){
                                commentBox = Qt.createComponent("commentBox.qml")
                                commentBox.createObject(boundingBoxRect,{"y": boundingBoxRect.height+5})
                            }
                    
                            MouseArea{
                                id: boundingBoxMouseArea
                                //properties
                                hoverEnabled: true
                                anchors.fill: parent
                    
                                //events
                                onEntered: {
                                    parent.border.color = onHoverColor;
                                    isHovered = true;
                                }
                                onExited: {
                                    parent.border.color = defaultColor;
                                    isHovered = false;
                                }
                            }
                        }
                    }
                    
                    p3c0P 1 Reply Last reply
                    0
                    • A Anas A. Ismail

                      @p3c0
                      img.qml

                      Item{
                         Canvas{
                              id: mainCanvase
                              width: 640
                              height: 480
                              visible: true
                              anchors.fill: parent
                      //        layer.enabled: false
                              BoundingBox{
                                  id: imgBox
                              }
                      
                              MouseArea{
                                  property Rectangle boundingBox: null
                                  property Component commentBox: null
                                  id: mainCanvaseMouseArea
                                  anchors.fill: parent
                      
                                  onPressed: {
                                     var boundingBoxObj = imgBox.createObject (mainCanvase, {"x" :mouse.x, "y": mouse.y});
                                      boundingBox = boundingBoxObj;
                                  }
                      
                                  onReleased: {
                                      if((boundingBox.width >2) && (boundingBox.height > 2))
                                      {
                                          boundingBox.createCommentBox();
                                          mainCanvaseMouseArea.enabled = false;
                                      }
                                      else
                                      {
                                          boundingBox.destroy();
                                      }
                                  }
                      
                                  onPositionChanged: {
                                      boundingBox.width = (Math.abs (mouse.x - boundingBox.x));
                                      boundingBox.height = (Math.abs (mouse.y - boundingBox.y));
                                  }
                              }
                          }
                      }
                      

                      boundingbox.qml

                      Component{
                          id: boundingBoxComponent
                      
                          Rectangle{
                              id: boundingBoxRect
                              objectName: "boundingBoxRect"
                              border.color: "red"
                              radius: 10
                              color: "transparent"
                              property color onHoverColor: "blue"
                              property color defaultColor: "red"
                              property bool isHovered: true
                              property Component commentBox: null
                      
                              function createCommentBox(){
                                  commentBox = Qt.createComponent("commentBox.qml")
                                  commentBox.createObject(boundingBoxRect,{"y": boundingBoxRect.height+5})
                              }
                      
                              MouseArea{
                                  id: boundingBoxMouseArea
                                  //properties
                                  hoverEnabled: true
                                  anchors.fill: parent
                      
                                  //events
                                  onEntered: {
                                      parent.border.color = onHoverColor;
                                      isHovered = true;
                                  }
                                  onExited: {
                                      parent.border.color = defaultColor;
                                      isHovered = false;
                                  }
                              }
                          }
                      }
                      
                      p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by p3c0
                      #10

                      @Anas-A.-Ismail According to your code property Rectangle boundingBox will always point to the last created or the latest created Item.

                      157

                      A 1 Reply Last reply
                      1
                      • p3c0P p3c0

                        @Anas-A.-Ismail According to your code property Rectangle boundingBox will always point to the last created or the latest created Item.

                        A Offline
                        A Offline
                        Anas A. Ismail
                        wrote on last edited by
                        #11

                        @p3c0 Thank You, your answers were really helpful, it's solved.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved