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. navigate image loaded by ListView
Forum Updated to NodeBB v4.3 + New Features

navigate image loaded by ListView

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 964 Views 1 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.
  • L Offline
    L Offline
    liubbc
    wrote on last edited by p3c0
    #1

    Hi Qters,

    I load some images by ListView and want to navigate these images using keyboard and mouse. The image will be scaled when navigating it . Now it works well using mouse, but there is something wrong using keyboard. The codes are as follows:

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.1
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Layouts 1.1
    
    
    Window {
        id: window
        visible: true
        width: 1000
        height: 400
        title: qsTr("Settings")
        color: "gray"
    
    
        ListModel{
            id: appsModel;
            ListElement{
                name: "Amazon"
                icon: "image/amazon.png"
            }
    
            ListElement{
                name: "TED"
                icon: "image/TED.png"
            }
    
            ListElement{
                name: "Netflix"
                icon: "image/netflix.png"
            }
        }
    
        Component{
            id: imageDelegate;
            Item {
                id: itemID;
                width: 250;
                height: 200;
                y: 100;
    
                Image {
                    id: appsImage;
                    source: icon;
                    width: 200;
                    height: 200;
    
                    fillMode: Image.PreserveAspectFit;
                    anchors.horizontalCenter: parent.horizontalCenter;
                }
    
                Text {
                    id: appsName;
                    text: name;
                    anchors.top: appsImage.bottom;
                    anchors.horizontalCenter: parent.horizontalCenter;
                }
    
                focus: true;
                transformOrigin: Item.Center;
    
                Keys.onPressed: {
                    if(event.key == Qt.Key_Left){
                        console.log("left is pressed");
    
                        itemID.scale = 1.1;
    
    
                    }else if(event.key == Qt.Key_Right){
                        console.log("right is pressed");
    
                        itemID.scale = 1.1;
                    }
                    //event.accepted = true;
    
                }
    
                Keys.onReleased: {
                    if(event.key == Qt.Key_Left){
                        console.log("left is pressed");
    
                        itemID.scale = 1.0;
    
    
                    }else if(event.key == Qt.Key_Right){
                        console.log("right is pressed");
    
                        itemID.scale = 1.0;
                    }
                   // event.accepted = true;
                }
    
                MouseArea{
                    hoverEnabled: true;
                    anchors.fill: appsImage;
                    onEntered: {
                        console.log("mouse is pressed");
                        itemID.scale = 1.1;
                    }
                    onExited: {
                        console.log("mouse is exited");
                        itemID.scale = 1.0;
                    }
                }
            }
        }
    
        ListView{
            id: appsListView;
            x: 50;
            width: parent.width - 50;
            height: parent.height;
    
            focus: true;
    
            orientation: ListView.Horizontal;
    
            model: appsModel;
            delegate: imageDelegate;
    
        }
    }
    

    The issue can be reproduced by modifying some lines :
    icon: "image/amazon.png"
    icon: "image/TED.png"
    icon: "image/netflix.png"

    Any suggestions about how to process Keys to solve the issue ? Thanks in advance !

    By the way, any other solutions to implement the requirement ? I am a novice about Qt .

    Thanks.

    Marco PellinM 1 Reply Last reply
    0
    • L liubbc

      Hi Qters,

      I load some images by ListView and want to navigate these images using keyboard and mouse. The image will be scaled when navigating it . Now it works well using mouse, but there is something wrong using keyboard. The codes are as follows:

      import QtQuick 2.6
      import QtQuick.Window 2.2
      import QtQuick.Controls 2.1
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Layouts 1.1
      
      
      Window {
          id: window
          visible: true
          width: 1000
          height: 400
          title: qsTr("Settings")
          color: "gray"
      
      
          ListModel{
              id: appsModel;
              ListElement{
                  name: "Amazon"
                  icon: "image/amazon.png"
              }
      
              ListElement{
                  name: "TED"
                  icon: "image/TED.png"
              }
      
              ListElement{
                  name: "Netflix"
                  icon: "image/netflix.png"
              }
          }
      
          Component{
              id: imageDelegate;
              Item {
                  id: itemID;
                  width: 250;
                  height: 200;
                  y: 100;
      
                  Image {
                      id: appsImage;
                      source: icon;
                      width: 200;
                      height: 200;
      
                      fillMode: Image.PreserveAspectFit;
                      anchors.horizontalCenter: parent.horizontalCenter;
                  }
      
                  Text {
                      id: appsName;
                      text: name;
                      anchors.top: appsImage.bottom;
                      anchors.horizontalCenter: parent.horizontalCenter;
                  }
      
                  focus: true;
                  transformOrigin: Item.Center;
      
                  Keys.onPressed: {
                      if(event.key == Qt.Key_Left){
                          console.log("left is pressed");
      
                          itemID.scale = 1.1;
      
      
                      }else if(event.key == Qt.Key_Right){
                          console.log("right is pressed");
      
                          itemID.scale = 1.1;
                      }
                      //event.accepted = true;
      
                  }
      
                  Keys.onReleased: {
                      if(event.key == Qt.Key_Left){
                          console.log("left is pressed");
      
                          itemID.scale = 1.0;
      
      
                      }else if(event.key == Qt.Key_Right){
                          console.log("right is pressed");
      
                          itemID.scale = 1.0;
                      }
                     // event.accepted = true;
                  }
      
                  MouseArea{
                      hoverEnabled: true;
                      anchors.fill: appsImage;
                      onEntered: {
                          console.log("mouse is pressed");
                          itemID.scale = 1.1;
                      }
                      onExited: {
                          console.log("mouse is exited");
                          itemID.scale = 1.0;
                      }
                  }
              }
          }
      
          ListView{
              id: appsListView;
              x: 50;
              width: parent.width - 50;
              height: parent.height;
      
              focus: true;
      
              orientation: ListView.Horizontal;
      
              model: appsModel;
              delegate: imageDelegate;
      
          }
      }
      

      The issue can be reproduced by modifying some lines :
      icon: "image/amazon.png"
      icon: "image/TED.png"
      icon: "image/netflix.png"

      Any suggestions about how to process Keys to solve the issue ? Thanks in advance !

      By the way, any other solutions to implement the requirement ? I am a novice about Qt .

      Thanks.

      Marco PellinM Offline
      Marco PellinM Offline
      Marco Pellin
      wrote on last edited by Marco Pellin
      #2

      @liubbc

      Hi,

      I'll give you my solution, but I guess there could be more than one.

      What I suggest is thinking that while changing the current element you are moving the focus from one item to the other. So let's think about linking the focus status to the image size, and this is done by using the onFocusChanged signal handler:

                  onFocusChanged: {
                      if(focus === true){
                          itemID.scale = 1.1;
                      }else{
                          itemID.scale = 1.0;
                      }
                  }
      

      With just this piece of code you are able to navigate with the keyboard.

      Now we have to add the mouse functionality. As you did, we add a MouseArea and entering it we have to give focus to the current image, while exiting the area we have to release the focus, like this:

                  MouseArea{
                      hoverEnabled: true;
                      anchors.fill: appsImage;
                      onEntered: {
                          console.log("mouse is pressed");
                          itemID.focus = true;
                      }
                      onExited: {
                          console.log("mouse is exited");
                          itemID.focus = false;
                      }
                  }
      

      In that way, you may also switch from mouse to keyboard maintaining consistency between the two methods.

      Here is the complete code I used:

      import QtQuick 2.6
      import QtQuick.Window 2.2
      import QtQuick.Controls 2.1
      import QtQuick.Controls.Styles 1.4
      import QtQuick.Layouts 1.1
      
      
      Window {
          id: window
          visible: true
          width: 1000
          height: 400
          title: qsTr("Settings")
          color: "gray"
      
      
          ListModel{
              id: appsModel;
              ListElement{
                  name: "Amazon"
                  icon: "image/amazon.png"
              }
              ListElement{
                  name: "TED"
                  icon: "image/TED.png"
              }
              ListElement{
                  name: "Netflix"
                  icon: "image/netflix.png"
              }
          }
      
          Component{
              id: imageDelegate;
              Item {
                  id: itemID;
                  width: 250;
                  height: 200;
                  y: 100;
      
                  Image {
                      id: appsImage;
                      source: icon;
                      width: 200;
                      height: 200;
      
                      fillMode: Image.PreserveAspectFit;
                      anchors.horizontalCenter: parent.horizontalCenter;
                  }
      
                  Text {
                      id: appsName;
                      text: name;
                      anchors.top: appsImage.bottom;
                      anchors.horizontalCenter: parent.horizontalCenter;
                  }
      
                  focus: false;
                  transformOrigin: Item.Center;
      
                  onFocusChanged: {
                      if(focus === true){
                          itemID.scale = 1.1;
                      }else{
                          itemID.scale = 1.0;
                      }
                  }
      
                  MouseArea{
                      hoverEnabled: true;
                      anchors.fill: appsImage;
                      onEntered: {
                          console.log("mouse is pressed");
                          itemID.focus = true;
                      }
                      onExited: {
                          console.log("mouse is exited");
                          itemID.focus = false;
                      }
                  }
              }
          }
      
          ListView{
              id: appsListView;
              x: 50;
              width: parent.width - 50;
              height: parent.height;
      
              focus: true;
      
              orientation: ListView.Horizontal;
      
              model: appsModel;
              delegate: imageDelegate;
          }
      }
      
      1 Reply Last reply
      0
      • L Offline
        L Offline
        liubbc
        wrote on last edited by
        #3

        @Marco-Pellin said in navigate image loaded by ListView:

        netflix.png

        @Marco-Pellin Thanks a lot!
        Now It works fine.

        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