将项目添加到自定义组件的布局

Avb

我有一个自定义设置Footer Component,希望在我的QML应用程序的其他位置重复使用:

Rectangle {
    color: "gold"
    height: 50
    anchors {
        bottom: parent.bottom
        left: parent.left
        right: parent.right
    }

    RowLayout {
        anchors.fill: parent
        anchors.margins: 10

        Button {
            text: "quit"
        }
    }
}

使用起来很简单:

Window {
    visible: true

    Footer {
    }
}

但是,现在我想在一个视图中RowLayout我的按钮添加一个“ ButtonA”,Footer在另一个视图中向其添加一个“ ButtonB”

我该如何实现?

米奇

看到这个答案。

您必须在中声明一个default属性Footer.qml

import QtQuick 2.0
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1

Rectangle {
    color: "gold"
    height: 50

    default property alias content: rowLayout.children

    anchors {
        bottom: parent.bottom
        left: parent.left
        right: parent.right
    }

    RowLayout {
        id: rowLayout
        anchors.fill: parent
        anchors.margins: 10

        Button {
            text: "quit"
        }
    }
}

这样可确保将任何声明为Footer实例子项的项添加到其子项中RowLayout

main.qml

import QtQuick 2.4
import QtQuick.Controls 1.3

ApplicationWindow {
    width: 640
    height: 480
    visible: true

    StackView {
        id: stackView
        anchors.fill: parent
        initialItem: viewAComponent
    }

    Component {
        id: viewAComponent

        Rectangle {
            id: viewA
            color: "salmon"

            Footer {
                id: footerA

                Button {
                    text: "Go to next view"
                    onClicked: stackView.push(viewBComponent)
                }
            }
        }
    }

    Component {
        id: viewBComponent

        Rectangle {
            id: viewB
            color: "lightblue"

            Footer {
                id: footerB

                Button {
                    text: "Go to previous view"
                    onClicked: stackView.pop()
                }
            }
        }
    }
}

我用作StackView在视图之间导航的便捷方式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法将项目添加到extjs中的自定义组件

Android-将自定义视图添加到xml布局中的组件

如何使用菜单布局将自定义项目添加到NavigationView?

将自定义布局添加到PreferenceFragment

使用自动布局将UIButton添加到自定义单元格

Android:如何动态地将“自定义视图”添加到线性布局中

Android:将OnClick处理程序添加到自定义首选项布局

使用自定义布局更新将块添加到类别

Android无法将子级添加到自定义布局(继承自ViewGroup)

将链接/模板列添加到自定义表组件

反应-如何将DOMAttribute添加到自定义组件?

通过自定义组件将 props 添加到 styled-component

将类添加到自定义组件angular 2

如何将Vanilla .js添加到自定义Vue组件

将 Background 属性添加到自定义 TextView 组件,底部只有边框

将附加类名添加到自定义React组件

将标签作为子级添加到自定义组件

如何将 Height 属性添加到自定义 React 组件

将标签和字段添加到自定义joomla组件

将项目从自定义模板添加到VS不会将其添加到TFS

将自定义字体添加到cordova项目

将项目的依赖添加到我的自定义gradle插件中

如何将项目动态添加到ListView的自定义适配器

无法使用自定义域将Firebase动态链接添加到项目

将@Future和LocalDate的自定义ConstraintValidator添加到Spring Boot项目

将新项目添加到自定义类的 Observable 集合中

无法将项目添加到ListView自定义BaseAdapter

将项目添加到自定义列表框

如何以编程方式将项目添加到 Sitecore 中的自定义索引?