1

我有一个主 qml 文件,其中包含顶部项目是一个矩形,中间项目是加载不同 qml 文件的加载器,底部项目是一些文本。

根据我希望底部项目应该调整的加载器项目,我尝试使用锚点,但有些它不起作用,有人可以解释我如何做到这一点。

这是我的代码:

main.qml

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Rectangle{
        anchors.fill: parent
        Rectangle{
            id: toprect
            width: 100
            height: 100
            color: "green"
            anchors.horizontalCenter: parent.horizontalCenter
        }
        Loader{
            id: middlerect
            anchors.top: toprect.bottom
            source: "qrc:/new.qml"
        }
        Rectangle{
            id: belowrect
            anchors.top: middlerect.bottom
            Text{
                text: "Bottom"
            }
        }
    }
}

新的.qml

import QtQuick 2.0
import QtQuick.Controls 1.2

Item {
    id: newid
    Column{
        spacing: 10
        Rectangle{
            width: 100
            height: 50
            color: "lightblue"
        }
        Rectangle{
            width: 100
            height: 50
            color: "lightgreen"
        }
    }
}

问题:

底部项目与中间项目重叠

4

1 回答 1

0

您的代码中有两个问题:

  1. 你应该给下面的矩形一个高度和宽度
  2. 你应该给你的 new.qml 对象一个高度和宽度

尝试这个:

main.qml

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Rectangle{
        anchors.fill: parent
        Rectangle{
            id: toprect
            width: 100
            height: 100
            color: "green"
            anchors.horizontalCenter: parent.horizontalCenter
        }


        Loader{
            id: middlerect
            anchors.top: toprect.bottom
            source: "qrc:/new.qml"
        }
        Rectangle{
            id: belowrect
            color:"yellow"

            width: childrenRect.width
            height: childrenRect.height
            anchors.top: middlerect.bottom
            Text{
                id:text
                text: "Bottom"
            }
        }
    }
}

新的.qml

import QtQuick 2.0
import QtQuick.Controls 1.2

Item {
    id: newid
    width: childrenRect.width
    height: childrenRect.height
    Column{
        spacing: 10
        Rectangle{
            width: 100
            height: 50
            color: "lightblue"
        }
        Rectangle{
            width: 100
            height: 50
            color: "lightgreen"
        }
    }
}
于 2015-10-14T21:36:22.460 回答