我正在嘗試制作一個可調整大小的對話框:
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.12
ApplicationWindow {
width: 640
height: 480
visible: true
title: qsTr("Test")
Dialog {
id: dlg
x: 10
y: 10
width: 100
height: 100
visible: true
Rectangle {
anchors.fill: parent
color: "blue"
}
MouseArea {
height: 40
width: 40
anchors.right: parent.right
anchors.bottom: parent.bottom
Rectangle {
anchors.fill: parent
color: "red"
}
property real startX: 0
property real startY: 0
property real startWidth: 0
property real startHeight: 0
onPressed: {
startX = mouseX;
startY = mouseY;
startWidth = dlg.width;
startHeight = dlg.height;
}
function fnc_updatePos() {
if (pressed) {
var deltaX = mouseX-startX;
var deltaY = mouseY-startY;
dlg.width = startWidth deltaX;
dlg.height = startHeight deltaY;
}
}
onPositionChanged: fnc_updatePos()
}
}
}
代碼會調整對話框的大小,但在拖動程序中對話框會閃爍。問題是滑鼠區域是對話框的一部分。如何改進代碼以正確縮放彈出對話框?
問候
uj5u.com熱心網友回復:
我發布答案而不是洗掉問題,以防萬一有人遇到同樣的問題。
mapToItem 是解決方案:
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Window 2.12
ApplicationWindow {
id: mainWindow
width: 640
height: 480
visible: true
title: qsTr("Test")
Dialog {
id: dlg
x: 10
y: 10
width: 100
height: 100
visible: true
Rectangle {
anchors.fill: parent
color: "blue"
}
MouseArea {
height: 40
width: 40
anchors.right: parent.right
anchors.bottom: parent.bottom
Rectangle {
anchors.fill: parent
color: "red"
}
property real startX: 0
property real startY: 0
property real startWidth: 0
property real startHeight: 0
onPressed: {
var pos = mapToItem(mainWindow.contentItem, mouseX, mouseY)
startX = pos.x;
startY = pos.y;
startWidth = dlg.width;
startHeight = dlg.height;
}
function fnc_updatePos() {
if (pressed) {
var pos = mapToItem(mainWindow.contentItem, mouseX, mouseY)
//console.log(pos)
var deltaX = pos.x-startX;
var deltaY = pos.y-startY;
dlg.width = startWidth deltaX;
dlg.height = startHeight deltaY;
}
}
onPositionChanged: fnc_updatePos()
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/387542.html
標籤:qt 质量管理器 qtquick2 qtquickcontrols2
上一篇:.NetCore5RazorpagesCookieAuthentication登錄成功后重定向到登錄頁面
下一篇:QML屬性的依賴評估順序
