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. How to set the Mouse Area for certain part of the image?
Forum Updated to NodeBB v4.3 + New Features

How to set the Mouse Area for certain part of the image?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 4.5k Views 2 Watching
  • 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.
  • Nisha_RN Offline
    Nisha_RN Offline
    Nisha_R
    wrote on last edited by Nisha_R
    #1

    For example i have am image of 400x400 but the rectangle in that image is only 100x100,
    i just want to set mouse area for that rectangle.
    how can i get it done in QT/QML?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stcorp
      wrote on last edited by
      #2

      Just have the MouseArea as a child of the image, and give the MouseArea a width and height of 100. If you need to place the MouseArea somewhere else in the image besides the top-left corner, use the x and y property of the MouseArea.

      The example below draws a 400x400 blue rectangle. I have placed a 100x100 MouseArea in the top right corner. When you click on it, it changes the color of the rectangle. In your case, just replace the Rectangle with Image

      Rectangle
      {
          width: 400
          height: 400
          color: "blue"
          MouseArea
          {
              x: 300
              y: 0
              width: 100
              height: 100
              onClicked: parent.color = Qt.hsva(Math.random(), 1, 0.7, 1)
          }
      }
      
      1 Reply Last reply
      0
      • Nisha_RN Offline
        Nisha_RN Offline
        Nisha_R
        wrote on last edited by Nisha_R
        #3

        Thank You @stcorp
        but, what if i have an arc in an image? and would want to set the mouse area?
        because the requirement is to arrange the arcs to form a circle, and on clicking an individual arc i should perform certain action.
        but on arranging the arcs the images are overlapping.

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @Nisha_R May be what you are look for is the masked mousearea.
          It is under <QtDir>/Examples/Qt-5.7/quick/customitems/maskedmousearea

          157

          1 Reply Last reply
          2
          • S Offline
            S Offline
            stcorp
            wrote on last edited by stcorp
            #5

            I'm not sure how to do this myself, but take a look at this, it seems like it could be a solution for you: https://forum.qt.io/topic/32045/non-rectangular-mouse-area-triangular-or-polygon-solved/8

            Edit: Oops, seems p3c0 already posted that solution while I was still searching for it :P

            1 Reply Last reply
            1
            • Nisha_RN Offline
              Nisha_RN Offline
              Nisha_R
              wrote on last edited by Nisha_R
              #6

              Thank you @p3c0 @stcorp ,
              Got it that was what i exactly wanted.

              S 1 Reply Last reply
              2
              • Nisha_RN Nisha_R

                Thank you @p3c0 @stcorp ,
                Got it that was what i exactly wanted.

                S Offline
                S Offline
                stcorp
                wrote on last edited by
                #7

                @Nisha_R

                You'll need a little bit of C++ to get this working.

                First, drop the maskedmousearea.cpp and maskedmousarea.h files into your project directory. Then in QtCreator, right click on your project and click "Add Existing Files..." and add these two files to your project.

                Next you need to make some edits to your main.cpp file. In the includes, include the following:

                #include "maskedmousearea.h"
                

                Now register this new type with the QML so you can use it in your QML code. This is done by pasting the following line into the main function of main.cpp (near the beginning of the function. It has to be placed before your QML is loaded):

                qmlRegisterType<MaskedMouseArea>("Example", 1, 0, "MaskedMouseArea");
                

                Now it is ready to be used in your QML. Import it using the following:

                import Example 1.0
                

                And then create the MaskedMouseArea using the following

                MaskedMouseArea {
                            id: maskedmousearea
                            anchors.fill: parent
                            alphaThreshold: 0.4
                            maskSource: parentimage.source
                        }
                

                But you should really have a look at the example if you want to see more about how it is implemented

                Nisha_RN 1 Reply Last reply
                1
                • S stcorp

                  @Nisha_R

                  You'll need a little bit of C++ to get this working.

                  First, drop the maskedmousearea.cpp and maskedmousarea.h files into your project directory. Then in QtCreator, right click on your project and click "Add Existing Files..." and add these two files to your project.

                  Next you need to make some edits to your main.cpp file. In the includes, include the following:

                  #include "maskedmousearea.h"
                  

                  Now register this new type with the QML so you can use it in your QML code. This is done by pasting the following line into the main function of main.cpp (near the beginning of the function. It has to be placed before your QML is loaded):

                  qmlRegisterType<MaskedMouseArea>("Example", 1, 0, "MaskedMouseArea");
                  

                  Now it is ready to be used in your QML. Import it using the following:

                  import Example 1.0
                  

                  And then create the MaskedMouseArea using the following

                  MaskedMouseArea {
                              id: maskedmousearea
                              anchors.fill: parent
                              alphaThreshold: 0.4
                              maskSource: parentimage.source
                          }
                  

                  But you should really have a look at the example if you want to see more about how it is implemented

                  Nisha_RN Offline
                  Nisha_RN Offline
                  Nisha_R
                  wrote on last edited by
                  #8

                  @stcorp thank you.

                  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