Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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
QML在纵向和横向之间旋转_Qml - Fatal编程技术网

QML在纵向和横向之间旋转

QML在纵向和横向之间旋转,qml,Qml,如果我旋转一个不同高度和宽度的矩形,我如何使它的窗口符合这些尺寸 我有一个根矩形(窗口本身)它包含QML中的一些其他矩形,我希望它可以从纵向旋转到横向,但当前旋转到横向时,窗口保持相同的大小,同时窗口的图像和内容旋转,并且在纵向模式下可见的部分内容丢失在窗口的边界之外,因为窗口不会调整为反映旋转 在示例代码中,绿色矩形lostone在窗口边界之外丢失,如果用户希望再次看到绿色矩形,则必须手动调整窗口大小或旋转回垂直方向。我想用背景矩形调整窗口大小 例如: import QtQuick 2.0

如果我旋转一个不同高度和宽度的矩形,我如何使它的窗口符合这些尺寸

我有一个根矩形(窗口本身)它包含QML中的一些其他矩形,我希望它可以从纵向旋转到横向,但当前旋转到横向时,窗口保持相同的大小,同时窗口的图像和内容旋转,并且在纵向模式下可见的部分内容丢失在窗口的边界之外,因为窗口不会调整为反映旋转

在示例代码中,绿色矩形
lostone
在窗口边界之外丢失,如果用户希望再次看到绿色矩形,则必须手动调整窗口大小或旋转回垂直方向。我想用
背景
矩形调整窗口大小

例如:

import QtQuick 2.0

Rectangle {
    id: screen
    width: 400
    height: 500
    state: "vertical"
    rotation: 0

    Rectangle {
        id: background
        width: 400
        height: 500
        anchors.centerIn: parent
        color: "blue"

        Rectangle {
            id: lostone
            width: 50
            height: 50
            color: "green"
            anchors.top: parent.top
        }

        Rectangle {
            id: rotater
            anchors.centerIn: parent
            width: 150
            height: 50
            color: "red"
            anchors.horizontalCenterOffset: 0
            smooth: true
            anchors.horizontalCenter: parent.horizontalCenter

            MouseArea {
                anchors.fill: parent
                onClicked: screen.toggle()
            }
        }

    }

    Behavior on width {
        NumberAnimation { duration: 100 }
    }

    Behavior on height {
        NumberAnimation { duration: 100 }
    }

    Behavior on rotation {
        NumberAnimation { duration: 100 }
    }

    function toggle() {
        console.log("toggle called, screen state is " + screen.state)
        if (screen.state=="vertical") {screen.state = "rotated"; } else { screen.state ="vertical"}
    }

    states: [
        State {
            name: "rotated"
            PropertyChanges {
                target: screen; rotation: -90; width: 400; height: 500

            }
        },
        State {
            name: "vertical"
            PropertyChanges {
                target: screen; rotation: 0; width: 400; height: 500
            }

        }

    ]
}
你对“根”这个词的使用有点混乱。Qt Quick场景中的根项目通常是窗口本身。我猜你指的是一个
矩形
,它是根项的子项?在任何情况下,最好计算您期望的最大尺寸:

import QtQuick 2.2

Rectangle {
    id: root
    width: Math.sqrt((rect.width * rect.width) + (rect.height * rect.height))
    height: width

    Rectangle {
        id: rect
        width: 400
        height: 200
        anchors.centerIn: parent

        gradient: Gradient {
            GradientStop {
                position: 0
                color: "red"
            }
            GradientStop {
                position: 1
                color: "orange"
            }
        }

        RotationAnimation {
            target: rect
            property: "rotation"
            from: 0
            to: 360
            running: true
            duration: 5000
            loops: Animation.Infinite
        }
    }
}
但是,如果矩形的大小在运行时更改,这将调整窗口的大小,因此最好使用固定大小:

width: Math.sqrt(400 * 400 + 200 * 200)
或者,只是估计一下:

width: 500

仅使用QML是不可能做到的

为了更改窗口的维度,需要从QML向本机主窗口对象发送一个信号,然后在信号处理程序中重新调整其大小


可以在main.cpp文件中找到主窗口对象

不,窗口就是我所说的根。这就是为什么我可以互换使用它们。很抱歉给你带来了困惑。将进行编辑以澄清。您能为您的问题添加一个简单的(有效的)示例吗?在中编辑。
屏幕
矩形(以及窗口)是否应该随状态改变其宽度和高度?有没有办法将父对象缩放到其子对象的大小?我尝试将屏幕的
高度
宽度
设置为
background.height
background.width
,但这不会改变窗口的大小。