我需要通過點擊和使用鍵盤輸入(上、下、左、右)來改變影像。當影像被選中時,顯示海報影像;未被選中時,顯示背景影像。我設法在點擊時改變它為海報影像,但當滑鼠不在當前影像上時,它需要回到背景影像。 以下是我的代碼:
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
width: 1920[/span].
高度。1080visible: true visible.
title: qsTr("Hello World"/span>)
Component.onCompleted: {
mojgrid.focus = true: !
}
function dobioResponseNapraviModel(response) {
console.log("dobioResponseNapraviModel"/span>, typeof response)
mojgrid.model=response
}
function request(){
console.log("BOK"/span>)
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.ready===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() {
如果(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。
/* Keys.onUpPressed: {
request()
}*/
delegate: Rectangle{ id: rect; width: 350; height: 400; color:'gray'
Image{id:image; width:parent. width; height:parent.height。
//fillMode: Image.PreserveAspectFit。
source:"http://image.tmdb.org/t/p/w400" modelData.backdrop_path。
MouseArea{
anchors.fill:father
onClicked: {
image.source="http://image.tmdb.org/t/p/w400" modelData.poster_path。
}
}
}
Text{ text:modelData.title; font.pointSize: 11}。
}
}
}
我需要檢測當前專案或當前索引在GridView中是否被選中,并基于此顯示不同的影像。我怎樣才能做到這一點呢?希望得到任何幫助。
uj5u.com熱心網友回復:
如果我對你的問題理解正確的話,你想在你按下滑鼠的時候得到一張圖片,而當你放手的時候得到另一張圖片。那么你只需要使用MouseArea的pressed屬性。
Image {
source: mouse.pressed ? pressedImage : unpressedImage
MouseArea{
id: mouse
anchors.fill: parent
}
更新:
要對鍵盤焦點進行同樣的操作,代碼非常相似:
Image {
source: focus ? pressedImage : unpressedImage
}
更新2:
將這兩個選項組合在一起,像這樣:
Image {
source: (focus || mouse.pressed) ? pressedImage : unpressedImage
MouseArea{
id: mouse
anchors.fill: parent
}
uj5u.com熱心網友回復:
在矩形屬性中我添加了:
。
onFocusChanged: {
if(focus) {
//console.log("fokus je na ", index, "mijenjam sliku")。
img.opacity=1.0。
img.source="http://image.tmdb.org/t/p/w400" modelData.poster_path。
}else{
img.source="http://image.tmdb.org/t/p/w400" modelData.backdrop_path
}
}
并且
keyNavigationEnabled: true在GridView。
這就解決了我的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/326136.html
標籤:
上一篇:如何轉移png
