Qt t、 高度 矩形{color:“yellow”;anchors.fill:parent}//仅显示效果 文本编辑{ clip:true//需要显式剪裁文本图形 id:文本编辑 正文:“知识产权” font.pixelSize:40*scaleFactor 锚定。填充:父级 } } }
但这只是一个变通方法,不是问题中要求的完整解决方案,因此问题仍然悬而未决。好的,这里有一个可能的变通方法。它不尝试控制窗口大小,而是在窗口中添加额外的项目,然后窗口包含UI的其余部分,并保持所需的纵横比:如果窗口纵横比不同,则内容区域的上下或左右都有空白区域,使用窗口颜色绘制(“此处为灰色”)。内容区域在此处用“黄色”填充以显示差异Qt t、 高度 矩形{color:“yellow”;anchors.fill:parent}//仅显示效果 文本编辑{ clip:true//需要显式剪裁文本图形 id:文本编辑 正文:“知识产权” font.pixelSize:40*scaleFactor 锚定。填充:父级 } } },qt,window,qml,qtquick2,aspect-ratio,Qt,Window,Qml,Qtquick2,Aspect Ratio,但这只是一个变通方法,不是问题中要求的完整解决方案,因此问题仍然悬而未决。好的,这里有一个可能的变通方法。它不尝试控制窗口大小,而是在窗口中添加额外的项目,然后窗口包含UI的其余部分,并保持所需的纵横比:如果窗口纵横比不同,则内容区域的上下或左右都有空白区域,使用窗口颜色绘制(“此处为灰色”)。内容区域在此处用“黄色”填充以显示差异 import QtQuick 2.4 import QtQuick.Window 2.2 Window { readonly property int
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
readonly property int defaultWidth: 700
readonly property int defaultHeight: 500
readonly property real defaultAspectRatio: defaultWidth / defaultHeight
readonly property real aspectRatio: width / height
readonly property real scaleFactor: content.width / defaultWidth
visible: true
width: defaultWidth
height: defaultHeight
color: "gray"
Item {
id: content
anchors.centerIn: parent
width: (aspectRatio > defaultAspectRatio)
? parent.height * defaultAspectRatio
: parent.width
height: (aspectRatio < defaultAspectRatio)
? parent.width / defaultAspectRatio
: parent.height
Rectangle { color: "yellow"; anchors.fill: parent } // just to show the effect
TextEdit {
clip: true // text drawing needs to be clipped explicitly
id: textEdit
text: "Lorem ipsum"
font.pixelSize: 40 * scaleFactor
anchors.fill: parent
}
}
}
导入QtQuick 2.4
导入QtQuick.Window 2.2
窗口{
只读属性int defaultWidth:700
只读属性int defaultHeight:500
只读属性real defaultAspectRatio:defaultWidth/defaultHeight
只读属性real aspectRatio:宽度/高度
只读属性real scaleFactor:content.width/defaultWidth
可见:正确
宽度:默认宽度
高度:默认高度
颜色:“灰色”
项目{
id:内容
anchors.centerIn:父对象
宽度:(aspectRatio>defaultAspectRatio)
?parent.height*defaultAspectRatio
:parent.width
高度:(aspectRatio
但这只是一个变通办法,不是问题中要求的完整解决方案,因此问题仍然悬而未决。谢谢您的建议。我想我会想保留框架,但我可能会做一些与您类似的事情:不允许正常调整大小,只需使用可切换的“正常大小”和“最大化大小”,而不是我自己的大小调整手柄。不过,我还是希望有人能给出一个完整的解决方案。唉,过滤掉调整大小事件并不能阻止窗口按我所想的那样调整大小。似乎可以通过将最大和最小高度和宽度设置为只允许一个大小来停止调整大小。停止调整大小不是问题,它可以重用具有自定义纵横比的windows框架大小调整栏。有多种方法禁止调整大小并保持窗口框架,但是一旦禁用,调整大小的处理停止响应用户输入,不能与之交互。@ HyDe -在这一点上,看起来问题不可能从QML解决,甚至不是从C++单独解决,而不是在私有API级别上重写窗口类。这看起来像是我在聊天会话中告诉你的Qt中的许多设计限制之一,行为是静态的和固定的,要重写的代码位于最不方便和最有问题的地方,需要修改。谢谢你的想法。我想我会想保留框架,但我可能会做一些与您类似的事情:不允许正常调整大小,只需使用可切换的“正常大小”和“最大化大小”,而不是我自己的大小调整手柄。不过,我还是希望有人能给出一个完整的解决方案。唉,过滤掉调整大小事件并不能阻止窗口按我所想的那样调整大小。似乎可以通过将最大和最小高度和宽度设置为只允许一个大小来停止调整大小。停止调整大小不是问题,它可以重用具有自定义纵横比的windows框架大小调整栏。有多种方法禁止调整大小并保持窗口框架,但是一旦禁用,调整大小的处理停止响应用户输入,不能与之交互。@ HyDe -在这一点上,看起来问题不可能从QML解决,甚至不是从C++单独解决,而不是在私有API级别上重写窗口类。这看起来像是我在聊天会话中告诉您的Qt中的许多设计限制之一,行为是静态的和固定的,要重写的代码位于最不方便和最有问题的地方进行修改。
Window {
id: main
visible: true
width: 500
height: 250
flags: Qt.FramelessWindowHint | Qt.Window
MouseArea {
width: 20
height: 20
anchors.bottom: parent.bottom
anchors.right: parent.right
onPositionChanged: {
var w = mouseX + x, h = mouseY + y
if (w > (h * 2)) {
main.width = w
main.height = w * .5
} else {
main.height = h
main.width = h * 2
}
}
Rectangle {
anchors.fill: parent
color: "red"
}
}
}
import QtQuick 2.4
import QtQuick.Window 2.2
Window {
readonly property int defaultWidth: 700
readonly property int defaultHeight: 500
readonly property real defaultAspectRatio: defaultWidth / defaultHeight
readonly property real aspectRatio: width / height
readonly property real scaleFactor: content.width / defaultWidth
visible: true
width: defaultWidth
height: defaultHeight
color: "gray"
Item {
id: content
anchors.centerIn: parent
width: (aspectRatio > defaultAspectRatio)
? parent.height * defaultAspectRatio
: parent.width
height: (aspectRatio < defaultAspectRatio)
? parent.width / defaultAspectRatio
: parent.height
Rectangle { color: "yellow"; anchors.fill: parent } // just to show the effect
TextEdit {
clip: true // text drawing needs to be clipped explicitly
id: textEdit
text: "Lorem ipsum"
font.pixelSize: 40 * scaleFactor
anchors.fill: parent
}
}
}