我想在Qt中执行与此图像非常相似的操作,我可以在任何方块中单击并更改其颜色。
答案 0 :(得分:0)
使用QML执行此操作非常简单。请看下面的代码:
import QtQuick 2.1
import QtQuick.Window 2.0
Window {
id: root
visible: true
width: 360
height: 500
Column{
Repeater{
model: getRowsNumber(root.height)
delegate: Row{
property int externalIdx: index
Repeater{
model: getColumnsNumber(root.width)
delegate: Rectangle{
property bool selected: false
property color originalColor: (index + externalIdx) % 2 == 0 ? "black" : "white"
width:20
height: 20
color: selected ? "red" : originalColor
border.width: 1
border.color: "black"
MouseArea{
anchors.fill: parent
onClicked: parent.selected = !parent.selected
}
}
}
}
}
}
function getColumnsNumber(width){
return width/20;
}
function getRowsNumber(height){
return height/20;
}
}
这就是你需要一个矩形的象棋网格,每个单元格在点击时会改变它的颜色。当然,您需要根据自己的需要进行调整,但这应该足以让您开始。