忽略矩形宽度,仅适用于anchors.fill:parent

时间:2015-10-26 12:43:00

标签: qml

在我的QML / Qt应用中,我关注Rectangle

import QtQuick 2.5
import QtQuick.Layouts 1.1

Rectangle
{
    id: uePlaceSelector

    signal ueSignalPlaceSelected
    signal ueSignalPlaceSelectionAborted

    property string ueSelectedPlaceName: ""

    radius: 16
    border.color: "#4682b4"
    border.width: 1
    antialiasing: true

    //FIXME how to set width same as UePlaceSwitcher's width?
    anchors.fill: parent

    gradient: Gradient
    {
        GradientStop
        {
            position: 0
            color: "#636363"
        }   // GradientStop

        GradientStop
        {
            position: 1
            color: "#303030"
        }   // GradientStop
    }   // Gradient

    ColumnLayout
    {
        antialiasing: true

        layoutDirection: Qt.LeftToRight

        spacing: 8

        anchors.fill: parent

        RowLayout
        {
            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.margins: 8

            Rectangle
            {
                id: uePlaceSelectorNavigatorBack

                Layout.preferredWidth: 32
                Layout.preferredHeight: 32

                color: "transparent"

                SequentialAnimation
                {
                    id: uePlaceSelectorNavigatorBackPressAnimation

                    loops: 1

                    running: false

                    NumberAnimation
                    {
                        target: uePlaceSelectorNavigatorBack

                        property: "opacity"

                        to: 0

                        duration: 150
                    }   // NumberAnimation

                    NumberAnimation
                    {
                        target: uePlaceSelectorNavigatorBack

                        property: "opacity"

                        to: 1

                        duration: 150
                    }   // NumberAnimation
                }   // SequentialAnimation

                Image
                {
                    anchors.fill: parent

                    asynchronous: true
                    horizontalAlignment: Image.AlignHCenter
                    verticalAlignment: Image.AlignVCenter

                    fillMode: Image.PreserveAspectFit

                    smooth: true

                    source: "qrc:///ueIcons/icons/ueArrowLeft.png"
                }   // Image

                MouseArea
                {
                    anchors.fill: parent

                    onClicked:
                    {
                        uePlaceSelectorNavigatorBackPressAnimation.running=true;
                        ueSignalPlaceSelectionAborted()
                    }   // onClicked
                }   // MouseAres
            }   // Rectangle
        }   // RowLayout

        GridView
        {
            id: uePlacesGridView

            antialiasing: true

            Layout.fillWidth: true
            Layout.fillHeight: true
            Layout.margins: 8
            Layout.alignment: Qt.AlignHCenter|Qt.AlignBottom
            flow: GridView.FlowLeftToRight

            layoutDirection: Qt.LeftToRight

            clip: true

            cellWidth: parent.width
            cellHeight: 128

            highlightFollowsCurrentItem: true
            highlightRangeMode: GridView.StrictlyEnforceRange

            snapMode: GridView.SnapToRow

            Component.onCompleted:
            {
                model=uePlacesModel
            }

            delegate: UeButton
            {
                id: uePlacesModelDelegate

                width: uePlacesGridView.cellWidth-16
                height: uePlacesGridView.cellHeight-16

                ueText: model.ueRoleName
                ueSoundOn: false

                onUeSignalButtonClicked:
                {
                    ueSelectedPlaceName=uePlacesGridView.model.get(index).ueRoleName;
                    ueSignalPlaceSelected()
                }   // onUeSignalButtonClicked:
            }   // delegate
        }   // GridView
    }   // ColumnLayout
}   // Rectangle

现在,这个Rectangle在启动时隐藏和禁用。但是在我的ApplicationWindowToolbar有海关Button,当用户按其中一个时,Rectangle会弹出OpacityAnimator。但由于anchors.fill: parent,其大小为全屏。它的父亲是ApplicationWindow,因为它是在它内部声明的。但是,我如何分配其宽度,因为

Component.onCompleted:
{
    ueLoggedUserPlaceSelector.width=uePlaceSwitcher.width
}   // Component.onCompleted
内部main.qml内的

不起作用。为什么呢?

1 个答案:

答案 0 :(得分:1)

替换

class RandomDistribution<T> {

    private class Event {
        public final T event;
        public final double relativeWeight;
        public Event(T event, double relativeWeight) {
            this.event = event;
            this.relativeWeight = relativeWeight;
        }
    }

    private double totalWeight = 0D;
    private ArrayList<Event> events = new ArrayList<>();
    private Random generator = new Random();

    public void addEvent(T event, double relativeWeight) {
        events.add(new Event(event, relativeWeight));
        totalWeight += relativeWeight;
    }

    public T randomEvent() {
        double random = generator.nextDouble() * totalWeight;
        for (Event event : events) {
            random -= event.relativeWeight;
            if (random < 0D) {
                return event.event;
            }
        }
        // It's possible to get here due to rounding errors
        return events.get(events.size() - 1).event;
    }

}

anchors.fill: parent

您应该width: uePlaceSwitcher.width height: uePlaceSwitcher.height 提供height,否则矩形将不可见。