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. Help with Mouse Area and Left and Right Click Selection in Qt Program
Forum Updated to NodeBB v4.3 + New Features

Help with Mouse Area and Left and Right Click Selection in Qt Program

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 604 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.
  • C Offline
    C Offline
    Collaxd
    wrote on 14 Mar 2023, 00:52 last edited by Collaxd
    #1

    I am trying to create a program that marks poker hands. I would like to click and drag the mouse over the grid of hands to select them. It would be great if I could use the right mouse button to set the selection back to default (i.e., clear the selection). Is there any solution for the mouse area? Thank you in advance.

    Below are some examples of how my program currently looks and how I would like it to be:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Controls 2.15
    import QtQuick.Layouts 1.15
    
    
    GridView {
        id: gridview
        property var hands: []
    
        function defaultBg(){
            return "lightgray"
        }
    
        function activeBg(){
            return "red"
        }
    
        model: hands
    
        anchors.horizontalCenter: parent.horizontalCenter
    
        cellWidth: width / 13
        cellHeight: height / 13
    
        delegate: Rectangle {
            width: gridview.cellWidth
            height: gridview.cellHeight
    
            border.width: 1
            border.color: "#444"
            color: defaultBg()
    
            Text {
                text: modelData
                font.pixelSize: 12
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                anchors.fill: parent
            }
    
            MouseArea {
                id: mouseArea
                anchors.fill: parent
                hoverEnabled: true
    
                onEntered: {
                    // if mouse left pressed set color
                    parent.color = activeBg()
                }
    
                onExited: {
                    // if mouse right pressed remove color
                    parent.color = defaultBg()
                }
            }
        }
    }
    

    alt text
    alt text

    S 1 Reply Last reply 14 Mar 2023, 03:03
    0
    • C Collaxd
      14 Mar 2023, 00:52

      I am trying to create a program that marks poker hands. I would like to click and drag the mouse over the grid of hands to select them. It would be great if I could use the right mouse button to set the selection back to default (i.e., clear the selection). Is there any solution for the mouse area? Thank you in advance.

      Below are some examples of how my program currently looks and how I would like it to be:

      import QtQuick 2.15
      import QtQuick.Window 2.15
      import QtQuick.Controls 2.15
      import QtQuick.Layouts 1.15
      
      
      GridView {
          id: gridview
          property var hands: []
      
          function defaultBg(){
              return "lightgray"
          }
      
          function activeBg(){
              return "red"
          }
      
          model: hands
      
          anchors.horizontalCenter: parent.horizontalCenter
      
          cellWidth: width / 13
          cellHeight: height / 13
      
          delegate: Rectangle {
              width: gridview.cellWidth
              height: gridview.cellHeight
      
              border.width: 1
              border.color: "#444"
              color: defaultBg()
      
              Text {
                  text: modelData
                  font.pixelSize: 12
                  horizontalAlignment: Text.AlignHCenter
                  verticalAlignment: Text.AlignVCenter
                  anchors.fill: parent
              }
      
              MouseArea {
                  id: mouseArea
                  anchors.fill: parent
                  hoverEnabled: true
      
                  onEntered: {
                      // if mouse left pressed set color
                      parent.color = activeBg()
                  }
      
                  onExited: {
                      // if mouse right pressed remove color
                      parent.color = defaultBg()
                  }
              }
          }
      }
      

      alt text
      alt text

      S Offline
      S Offline
      supper_DM
      wrote on 14 Mar 2023, 03:03 last edited by
      #2

      @Collaxd

          MouseArea{
              anchors.fill: parent
              acceptedButtons: Qt.LeftButton | Qt.RightButton
              onClicked: {
                  if(mouse.button === Qt.LeftButton)
                  {
                  }
                  else if(mouse.button === Qt.RightButton)
                  {
                  }
              }
          }
      
      C 1 Reply Last reply 15 Mar 2023, 15:33
      0
      • S supper_DM
        14 Mar 2023, 03:03

        @Collaxd

            MouseArea{
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton | Qt.RightButton
                onClicked: {
                    if(mouse.button === Qt.LeftButton)
                    {
                    }
                    else if(mouse.button === Qt.RightButton)
                    {
                    }
                }
            }
        
        C Offline
        C Offline
        Collaxd
        wrote on 15 Mar 2023, 15:33 last edited by Collaxd
        #3

        @supper_DM said in Help with Mouse Area and Left and Right Click Selection in Qt Program:

            acceptedButtons: Qt.LeftButton | Qt.RightButton
        

        Hello friend, thank you for your response. I just think that my question was not well understood. I need to click and drag the mouse while clicking and painting the slots as if it were in Excel.

        I can do something similar without accepting any mouse button, but inside my "entered" I need it to check the button state if it is left or right pressed.

            MouseArea{
                anchors.fill: parent
        //        acceptedButtons: Qt.LeftButton | Qt.RightButton
                acceptedButtons: Qt.NoButton
                hoverEnabled: true
        
                onEntered: {
                    parent.color = "orange"
                }
        
                onExited: {
                    parent.color = sys.defaultHandBackgroundColor
                }
            }
        
        1 Reply Last reply
        0

        2/3

        14 Mar 2023, 03:03

        • Login

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