Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QML自动锚定多个项目_Qt_Qml - Fatal编程技术网

Qt QML自动锚定多个项目

Qt QML自动锚定多个项目,qt,qml,Qt,Qml,我想以编程方式使用锚点将几个项目排成一行。另外,我希望一个项目被拉伸,因此我不设置这个特定项目的宽度。看起来QML忽略了我为项目设置的固定宽度 C++代码: #include <QGuiApplication> #include <QQuickView> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGui

我想以编程方式使用锚点将几个项目排成一行。另外,我希望一个项目被拉伸,因此我不设置这个特定项目的宽度。看起来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: 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)

我的问题是,如何在不键入每个项目和锚的情况下实现我的目标?

您是否考虑过使用
QtQuick.Controls.Layout
?我想你可以不费吹灰之力地做你想做的事情,只是想要一个简单的容器,我把东西放在里面,容器会处理(重新)大小。布局要求每个项目都有很多额外的步骤,如最小/最大/首选宽度。此外,我还缺少Qt小部件中的拉伸因子。锚不知道
width
是否是硬编码的。您必须向
StretchingRow
属性提供拉伸项目的相关信息(例如
stretchingColumn
属性),并将索引相同或更低的所有内容锚定到左侧同级(或索引为0的父项),将索引相同或更高的所有内容锚定到右侧同级(或最后一个项目的父项)。我还希望没有绑定,
宽度
上的
update()
更改是必要的,因为
项。左
不是
x
位置,而是锚定目标。相反,当
children
属性发生更改时,您可能希望执行
更新()
。@johannestomokic,您是对的!非常感谢。您是否考虑过使用
QtQuick.Controls.Layout
?我想你可以不费吹灰之力地做你想做的事情,只是想要一个简单的容器,我把东西放在里面,容器会处理(重新)大小。布局要求每个项目都有很多额外的步骤,如最小/最大/首选宽度。此外,我还缺少Qt小部件中的拉伸因子。锚不知道
width
是否是硬编码的。您必须向
StretchingRow
属性提供拉伸项目的相关信息(例如
stretchingColumn
属性),并将索引相同或更低的所有内容锚定到左侧同级(或索引为0的父项),将索引相同或更高的所有内容锚定到右侧同级(或最后一个项目的父项)。我还希望没有绑定,
宽度
上的
update()
更改是必要的,因为
项。左
不是
x
位置,而是锚定目标。相反,当
children
属性发生更改时,您可能希望执行
更新()
。@johannestomokic,您是对的!非常感谢。
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)