我想要实现的是,如果用户点击其中一个图像,它应该带我到另一个QML页面,并且在按ESC时,它应该返回到主QML文件.
此外,如果用户重新调整窗口大小,则图像应根据窗口的大小自行排列.
即窗口缩小时的小图像和窗口拉伸时的较大图像.
这可能吗?如果是,我如何确保图标会在更改窗口大小时更改其大小,单击它会将用户带到另一个页面?
几天以来一直在使用QML,真的无法获得太多信息......谢谢!
以下是如何使用QML/Qt5执行此类行为的示例.它使用多个组件来完成所有这些:
干得好:
import QtQuick 2.1
import QtQuick.Controls 1.0
ApplicationWindow {
title: qsTr("Hello World")
width: 400
height: 400
// Use the Grid component to display your images
Grid {
id: grid;
// You want the grid to be as big a the window so that
// it follows the resizing
anchors.fill: parent
// Add spacing between images
rowSpacing: 5
columnSpacing: 5
columns: 3
rows: 3
// Use a Repeater to repeat your images
Repeater {
// 5 * 5 elements here
model: grid.columns * grid.rows;
// Component to use to represent each image you want
delegate: delegateGridImage
}
}
// Component that will represent each Image you want in your grid.
// Right now I use Rectangle because it is easier for an example
Component {
id: delegateGridImage
Item {
// For each Item we remember on which row and column it is
// index is a property of each delegate in a Repeater
// corresponding to the current index of the element
property int currentColumn: index % grid.columns
property int currentRow: Math.floor(index / grid.rows);
// We want the Item to grow with the grid
width: grid.width / grid.columns
height: grid.height / grid.rows
Rectangle {
anchors.fill: parent
color: index % 2 == 0 ? "red" : "blue"
// Add a text to show how each Item can be different
Text {
anchors.centerIn: parent
text: "Col " + currentColumn + " - Row " + currentRow
}
}
// Add a MouseArea in each element that hide the grid when clicked
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: {
fakeViewText.text = "The element clicked was" +
"[" + currentRow + "," + currentColumn + "]"
grid.visible = false;
}
}
}
}
// Add a Text element in the middle to fake a new "view"
// Will display which element was clicked and be hidden
// as soon as we hit escape
Text {
id: fakeViewText
// The text is visible only when the grid is not
visible: !grid.visible
anchors.centerIn: parent
text: ""
// Enable keyboard detection only if the fakeView is visible
Keys.enabled: visible
// Give focus to the element when visible to have keys detection
focus: visible
// Bring back the grid when escape is pressed
Keys.onEscapePressed: {
grid.visible = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)