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 focus for the items on mouse movement - List View
Forum Updated to NodeBB v4.3 + New Features

How to set focus for the items on mouse movement - List View

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 4 Posters 2.2k 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.
  • J Offline
    J Offline
    JasmineSethi
    wrote on last edited by JasmineSethi
    #1

    How to set focus/highlight for the items(red,blue green) on selection?

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick 2.12
    import QtQuick.Controls 2.12

    Rectangle {
    width: 800
    height: 480

    Rectangle {
        id:comboBox
        property variant items: ["Red", "Blue", "Green"]
    
        signal comboClicked;
        anchors.centerIn: parent
        width: 141
        height: 30;
        z: 0
        smooth:true;
    
    
        Rectangle
        {
            id:chosenItem
            radius:4;
            width:100;
            height:30;
            color: "#454b4d"
            smooth:true;
    
            Image
            {
                id: name
                source: "Menu_Button_Dropdown_Enabled_Medium.png"
                anchors.fill: parent
            }
    
            Text {
                id:chosenItemText
                anchors.centerIn: parent
                color: "black"
                text:"Abc";
                anchors.topMargin: 5
                anchors.left: parent.left
                anchors.leftMargin: 12
                font.family: "Arial"
                font.pointSize: 14;
                smooth:true
            }
    
            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    comboBox.state = comboBox.state==="dropDown"?"":"dropDown"
                }
            }
        }
    
        Rectangle
        {
            id:dropDown
            width:100;
            height:0;
            clip:true;
            border.color: "black"
            anchors.bottom:  chosenItem.top;
            anchors.margins: 2;
    
            ListView {
                id:listView
                height:1000;
                model: comboBox.items
                currentIndex: 0
    
    
                delegate: Item{
                    width:comboBox.width;
                    height: comboBox.height;
    
    
    
    
                    Text {
                        text: modelData
                        anchors.top: parent.top;
                        anchors.left: parent.left;
                        anchors.margins: 5;
                        color: "black"
                        elide: modelData.elideMode
    
                    }
                    MouseArea {
                        anchors.fill: parent;
                        hoverEnabled: true
                        onClicked: {
    
                            comboBox.state = ""
                            chosenItemText.text = modelData;
                            listView.currentIndex = index;
                        }
                    }
                }
            }
        }
    
    
        states: State {
            name: "dropDown";
            PropertyChanges { target: dropDown; height:30*comboBox.items.length }
        }
    
        transitions: Transition {
            NumberAnimation { target: dropDown; properties: "height"; easing.type: Easing.OutExpo; duration: 1000 }
        }
    }
    

    }

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Focus means highlighter ? Did you try to change the currentIndex based on the mouse movement ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      J 1 Reply Last reply
      0
      • dheerendraD dheerendra

        Focus means highlighter ? Did you try to change the currentIndex based on the mouse movement ?

        J Offline
        J Offline
        JasmineSethi
        wrote on last edited by
        #3

        @dheerendra Yes, to highlight the sub items on clicked. Yes i changed the currentIndex . But still not getting how to add this highlight?

        ODБOïO 1 Reply Last reply
        0
        • J JasmineSethi

          @dheerendra Yes, to highlight the sub items on clicked. Yes i changed the currentIndex . But still not getting how to add this highlight?

          ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          @JasmineSethi hi,
          simply add this to your ListView s delegate. With this the selected index will have a blue rectangle highlight

          Rectangle{id:highlighter; anchors.fill: parent; color: "blue"; visible: listView.currentIndex===index}
          

          Note : in your title you said "on mouse movement" but in the body "on selection"

          J 1 Reply Last reply
          2
          • YunusY Offline
            YunusY Offline
            Yunus
            wrote on last edited by
            #5

            Yeah this code can be used but there can be some problems because of delegate

            1 Reply Last reply
            0
            • ODБOïO ODБOï

              @JasmineSethi hi,
              simply add this to your ListView s delegate. With this the selected index will have a blue rectangle highlight

              Rectangle{id:highlighter; anchors.fill: parent; color: "blue"; visible: listView.currentIndex===index}
              

              Note : in your title you said "on mouse movement" but in the body "on selection"

              J Offline
              J Offline
              JasmineSethi
              wrote on last edited by JasmineSethi
              #6

              @LeLev

              0_1549256390126_15a43873-3838-4dc2-bb74-e95685e886e0-image.png

              After adding the above code output is this. By default first index is of blue color and its not changing even if i am using other index values. I dont want this. I want when i will select any index it has to become blue color.

              1 Reply Last reply
              0
              • YunusY Offline
                YunusY Offline
                Yunus
                wrote on last edited by Yunus
                #7

                @JasmineSethi
                Did you put lelev's code right after delegate? I tried this and it is working well. When you clicked blue or green, only the clicked one become blue.

                J 1 Reply Last reply
                0
                • YunusY Yunus

                  @JasmineSethi
                  Did you put lelev's code right after delegate? I tried this and it is working well. When you clicked blue or green, only the clicked one become blue.

                  J Offline
                  J Offline
                  JasmineSethi
                  wrote on last edited by
                  #8

                  @Yunus

                  0_1549258706596_866fa26f-a350-435c-b8c4-448eb34ccf09-image.png

                  I added like this. Is this fine??

                  1 Reply Last reply
                  0
                  • YunusY Offline
                    YunusY Offline
                    Yunus
                    wrote on last edited by
                    #9

                    @JasmineSethi Yes I tried exactly this and it is ok. I don't know that is a problem for you by default the red part is blue firstly. But when you clicked the others then, they become blue and the others blue color disappear.

                    J 1 Reply Last reply
                    0
                    • YunusY Yunus

                      @JasmineSethi Yes I tried exactly this and it is ok. I don't know that is a problem for you by default the red part is blue firstly. But when you clicked the others then, they become blue and the others blue color disappear.

                      J Offline
                      J Offline
                      JasmineSethi
                      wrote on last edited by
                      #10

                      @Yunus
                      Did u make any changes other than "highlighter" rectangle??

                      1 Reply Last reply
                      0
                      • YunusY Offline
                        YunusY Offline
                        Yunus
                        wrote on last edited by
                        #11

                        @JasmineSethi said in How to set focus for the items on mouse movement - List View:

                        ort QtQuick 2.12
                        import QtQuick.Window 2.12

                        @JasmineSethi
                        No I changed nothing I just copied your codes. only difference is that I used

                        import QtQuick 2.9
                        import QtQuick.Window 2.2

                        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