Я хочу выложить несколько элементов подряд с помощью якорей программно. Также я хочу растянуть один элемент, поэтому я не устанавливаю ширину для этого конкретного элемента. Похоже, QML игнорирует фиксированную ширину, которую я установил для элемента.
Код C ++:
#include <QGuiApplication>
#include <QQuickView>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QStringLiteral("qrc:/main.qml"));
view.setWidth(640);
view.setHeight(480);
view.setTitle("QML auto anchors");
view.show();
return app.exec();
}
Код QML растяжения строки:
import QtQuick 2.0
import QtQuick.Controls 2.12
Rectangle {
id: root
function update() {
console.log("----------------------")
console.log("update called, children count: "+children.length)
for (var i=0;i<children.length;i++) {
console.log("parent: "+children[i].parent)
console.log(i + " child: " + children[i] + "," + children[i].implicitWidth + "," + children[i].width)
if(i===0) {
console.log("first child: "+children[i])
children[i].anchors.left = Qt.binding(function() { return children[i].parent.left })
} else if(i===children.length-1) {
console.log("last child: "+children[i])
children[i].anchors.right = Qt.binding(function() { return children[i].parent.right } )
} else {
console.log("inbetween child: "+children[i] + ", left sibling: " + children[i-1] + ", right sibling: " + children[i+1])
children[i].anchors.left = Qt.binding(function() { return children[i-1].right })
children[i].anchors.right = Qt.binding(function() { return children[i+1].left })
}
}
}
onWidthChanged: update()
Component.onCompleted: update()
}
main.qml
import QtQuick 2.12
import QtQuick.Controls 2.12
Rectangle {
anchors.fill: parent
color: "red"
StretchingRow {
width: parent.width
height: 30
color: "purple"
// width based on content
Label {
text: "Label"
}
// i want this to be stretched
SpinBox {
height: 30
value: 100
}
// fixed width
Button {
width: 30
height: 30
text: "B1"
}
// fixed width
Button {
width: 30
height: 30
text: "B2"
}
}
}
Это вывод журнала консоли:
qml: update called, children count: 4
qml: parent: StretchingRow_QMLTYPE_0(0x2ab0be81f50)
qml: 0 child: QQuickLabel(0x2ab0be7f0f0),26,26
qml: first child: QQuickLabel(0x2ab0be7f0f0)
qml: parent: StretchingRow_QMLTYPE_0(0x2ab0be81f50)
qml: 1 child: QQuickSpinBox(0x2ab0bfdd230),140,-26
qml: inbetween child: QQuickSpinBox(0x2ab0bfdd230), left sibling: QQuickLabel(0x2ab0be7f0f0), right sibling: QQuickButton(0x2ab0bfe8950)
qml: parent: StretchingRow_QMLTYPE_0(0x2ab0be81f50)
qml: 2 child: QQuickButton(0x2ab0bfe8950),100,610
qml: inbetween child: QQuickButton(0x2ab0bfe8950), left sibling: QQuickSpinBox(0x2ab0bfdd230), right sibling: QQuickButton(0x2ab0bfea700)
qml: parent: StretchingRow_QMLTYPE_0(0x2ab0be81f50)
qml: 3 child: QQuickButton(0x2ab0bfea700),100,30
qml: last child: QQuickButton(0x2ab0bfea700)
Мой вопрос заключается в том, как достичь своей цели, не набирая все элементы и привязки?