This qml code, generates grid layouted filled with random values
此QML代码生成填充了随机值的格网布局
GridLayout {
id: month
objectName: "month"
height: Screen.height - 500
width: Screen.width - 300
rows: 16
columns: numberOfDays
columnSpacing: 0
rowSpacing: 0
x: 150
y: 350
Repeater {
model: month.rows * month.columns
id: monthR
delegate: Rectangle {
width: month.width / month.columns
height: month.height / month.rows
border.color: "black"
border.width: 1
color: saturdays.includes(index % month.columns) ? "lightblue" : ( sundays.includes(index % month.columns) ? "pink" : (index >= month.columns * 8 && index < month.columns * 9 ? "lightgrey" : "white"))
TextEdit {
anchors.fill: parent
text: index >= month.columns * 8 && index < month.columns * 9 ? "-" : Math.floor(Math.random() * 10) + 1
readOnly: index >= month.columns * 8 && index < month.columns * 9
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pixelSize: 25
}
}
}
}
c++
C++
QObjectList children = rootObject->children();
QQuickItem *gridLayout = nullptr;
for (QObject *child : children) {
if (child->objectName() == "month") {
gridLayout = qobject_cast<QQuickItem*>(child);
qDebug() << gridLayout;
}
}
qDebug() << gridLayout->childAt(0,0)->children().at(0)->property("text");
qDebug() << gridLayout->childAt(0,1)->children().at(0)->property("text");
qDebug() << gridLayout->childAt(0,2)->children().at(0)->property("text");
qDebug() << gridLayout->childAt(0,3)->children().at(0)->property("text");
no matter what I try value readen is always from childAt(0,0)
无论我尝试什么,Value Reden总是从小At(0,0)
Instructions below return:
返回以下说明:
qDebug() << gridLayout->children() ----- QList(QQuickRepeater(0x25929912640), QQuickScreenAttached(0x25929b1df00), QQuickLayoutAttached(0x2592c8a5da0))
qDebug() << gridLayout->childAt(0,0)->children(); ------QList(QQuickPre64TextEdit(0x25929b45c50), QQuickPen(0x25929b1f6c0), QQuickLayoutAttached(0x2592c8a6340))
I try different ways to read those values
我尝试了不同的方式来解读这些价值观
更多回答
The input parameters for the Item.childAt
function aren't row
and column
indexes; but, they are x
and y
coordinates. The function returns the child at the point(x, y)
. Which for small numbers, the result will always be the first child. To get child[i][j]
, try this: children[i * columns + j]
or use a table view and model.
函数的输入参数不是行和列索引,而是x和y坐标。该函数返回点(x,y)处的子级。对于较小的数字,结果将始终是第一个孩子。要获取Child[i][j],请尝试这样做:Child[i*Columns+j]或使用表视图和模型。
我是一名优秀的程序员,十分优秀!