【问题标题】:How to change focus in GridView on key navigation QML如何在 GridView 中更改对关键导航 QML 的关注
【发布时间】:2021-09-17 10:40:42
【问题描述】:

我正在尝试在 GridView 中按下键(左、右、上和下)时更改图像的焦点。当我按左,右,上或下键时,我应该改变图像焦点,并在该图像上的焦点为真时改变图像。否则,如果焦点不在图像上,则应该看到旧图像。

这是我现在所拥有的:

这是我的代码:

import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 1920
    height: 1080
    visible: true
    title: qsTr("Hello World")
    Component.onCompleted: {
        mojgrid.focus = true
    }

    function dobioResponseNapraviModel(response) {
        console.log("dobioResponseNapraviModel", typeof response)

        mojgrid.model=response
    }

    function request(){
        console.log("BOK")

        const xhr=new XMLHttpRequest()
        const method="GET";
        const url="http://api.themoviedb.org/4/list/1";
        xhr.open(method, url, true);
        xhr.setRequestHeader( "Authorization", 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI5YjBkOGVlMGQzODdiNjdhYTY0ZjAzZDllODM5MmViMyIsInN1YiI6IjU2MjlmNDBlYzNhMzY4MWI1ZTAwMTkxMyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.UxgW0dUhS62m41KjqEf35RWfpw4ghCbnSmSq4bsB32o');
        xhr.onreadystatechange=function(){
            if(xhr.readyState===XMLHttpRequest.DONE){
                var status=xhr.status;
                if(status===0 || (status>=200 && status<400)){
                    //the request has been completed successfully
//                    console.log(xhr.responseText.results)
                    dobioResponseNapraviModel(JSON.parse(xhr.responseText).results)
               }else{
                    console.log("There has been an error with the request", status, JSON.stringify(xhr.responseText))
                }
            }
        }
        xhr.send();
    }





   /* function request(url, callback) {
        var xhr=new XMLHttpRequest();

        xhr.open("GET", url, true)

        xhr.onreadystatechange = function() {
            if(xhr.readyState===4) {


                callback(xhr.responseText)


            }
        }
        xhr.open("GET", url)
        xhr.setRequestHeader( "Authorization", 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI5YjBkOGVlMGQzODdiNjdhYTY0ZjAzZDllODM5MmViMyIsInN1YiI6IjU2MjlmNDBlYzNhMzY4MWI1ZTAwMTkxMyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.UxgW0dUhS62m41KjqEf35RWfpw4ghCbnSmSq4bsB32o');
        xhr.send()

    }*/




    GridView {
        id:mojgrid
        anchors.fill: parent
        cellWidth: 250
        cellHeight: 250
        model:request()
        currentIndex: modelData.id
        keyNavigationEnabled: true
        focus:true


        Keys.onPressed:{
                    if((event.key === Qt.Key_Left) || (event.key===Qt.Key_Right) || (event.key===Qt.Key_Up) || (event.key===Qt.Key_Down)){
                        image.source="http://image.tmdb.org/t/p/w400"+modelData.poster_path
                   }
        }

       /* Keys.onUpPressed: {
           request()
        }*/
      delegate: Rectangle{ id: rect; width: 350; height: 400; color:'gray';
          Image{id:img; width:parent.width; height:parent.height-200
                 //fillMode: Image.PreserveAspectFit
                 //source:"http://image.tmdb.org/t/p/w400" + modelData.backdrop_path
                source:focus?"http://image.tmdb.org/t/p/w400"+modelData.poster_path : "http://image.tmdb.org/t/p/w400" + modelData.backdrop_path

               Rectangle{
                   id:rect2
                   width:parent.width
                   height:text.height
                   anchors.top:img.bottom
                   color:'black'
                 Text{
                       id:text
                       text:modelData.title
                       font.pointSize: 11
                       //anchors.top:image.bottom
                       elide:Text.ElideNone
                       color:'white'
                   }

                }

                MouseArea{
                id:mouse
                anchors.fill:parent

                onClicked: {
                    parent.focus=true
                }
              }



          }

          Rectangle{
              id:rect3
              width:parent.width
              height:200
              anchors.top:rect.bottom
              color:'red'
              z:10

              Text{
                  text:modelData.release_date
                  anchors.left:rect.left
                  anchors.top:rect.bottom
                  color: 'white'

              }
          }





      }
    }
  }

我在这里有 Keys.onPressed 和 if 条件,但它不起作用。有人可以帮我吗?

【问题讨论】:

    标签: image qt gridview qml


    【解决方案1】:

    我稍微简化并重新格式化了您的代码,但是这段代码 sn-p 正在做您想做的事情。当启用键导航时,GridView 会自行处理索引更新。实际上键导航正在工作,当您按下键时,当前索引会更新。 GridView 还处理导航限制,当您在最后一行按下时,不会发生与在第一列上按下左时相同的情况。诀窍是使用currentindex 更新图像。

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        width: 1920
        height: 1080
        visible: true
        title: qsTr("Hello World")
    
    
        function dobioResponseNapraviModel(response) {
            console.log("dobioResponseNapraviModel", typeof response)
    
            mojgrid.model = response
        }
    
        GridView {
            id: mojgrid
            anchors.fill: parent
            cellWidth: 250
            cellHeight: 250
            model: request()
    
    
            keyNavigationEnabled: true
            focus: true
    
            delegate: Rectangle {
                id: rect
                width: 350
                height: 400
                color: 'gray'
    
                property bool isCurrent: mojgrid.currentIndex === index
    
                Image {
                    id: img
                    width: parent.width
                    height: parent.height - 200
    
                    source: isCurrent ? "http://image.tmdb.org/t/p/w400"
                                    + modelData.poster_path : "http://image.tmdb.org/t/p/w400"
                                    + modelData.backdrop_path
    
                    Rectangle {
                        id: rect2
                        width: parent.width
                        height: text.height
                        anchors.top: img.bottom
                        color: 'black'
                        Text {
                            id: text
                            text: modelData.title
                            font.pointSize: 11
                            //anchors.top:image.bottom
                            elide: Text.ElideNone
                            color: 'white'
                        }
                    }
    
                    MouseArea {
                        id: mouse
                        anchors.fill: parent
    
                        onClicked: {
                            mojgrid.currentIndex = index
                        }
                    }
                }
            }
        }
    
        function request() {
            console.log("BOK")
    
            const xhr = new XMLHttpRequest()
            const method = "GET"
            const url = "http://api.themoviedb.org/4/list/1"
            xhr.open(method, url, true)
            xhr.setRequestHeader(
                        "Authorization",
                        'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI5YjBkOGVlMGQzODdiNjdhYTY0ZjAzZDllODM5MmViMyIsInN1YiI6IjU2MjlmNDBlYzNhMzY4MWI1ZTAwMTkxMyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.UxgW0dUhS62m41KjqEf35RWfpw4ghCbnSmSq4bsB32o')
            xhr.onreadystatechange = function () {
                if (xhr.readyState === XMLHttpRequest.DONE) {
                    var status = xhr.status
                    if (status === 0 || (status >= 200 && status < 400)) {
                        dobioResponseNapraviModel(JSON.parse(
                                                      xhr.responseText).results)
                    } else {
                        console.log("There has been an error with the request",
                                    status, JSON.stringify(xhr.responseText))
                    }
                }
            }
            xhr.send()
        }
    }
    

    【讨论】:

    • 嗨 Muhhammet Ali Asan,非常感谢,这很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多