QML之自定义模态可拖拽窗口
2017-12-21 22:11
281 查看
效果
代码
main.qml
import QtQuick 2.5 import Qt.labs.controls 1.0 ApplicationWindow { id: root visible: true width: 840 height: 680 title: qsTr("Hello World") Button{ id: btn anchors.centerIn: parent text: "click" onClicked: { myDlg.open() myDlg.x = parent.width/2 - myDlg.width/2 myDlg.y = parent.height/2 - myDlg.height/2 } } MyDialog{ id: myDlg } }
MyDialog.qml
import QtQuick 2.0 //Qt5.6之前版本Popop包含在Qt Labs Controls模块中,之后版本Popop包含在Qt Quick Controls模块中 import Qt.labs.controls 1.0 Popup { id: root x: parent.width/2 - root.width/2 y: parent.height/2 - root.height/2 width: 530 height: 300 modal: true focus: true //设置窗口关闭方式为按“Esc”键关闭 closePolicy: Popup.OnEscape //设置窗口的背景控件,不设置的话Popup的边框会显示出来 background: rect Rectangle { id: rect anchors.fill: parent color: "#87C056" border.width: 2 opacity: 1 radius: 8 Rectangle{ width: parent.width-4 height: 2 anchors.top: parent.top anchors.topMargin: 40 anchors.left: parent.left anchors.leftMargin: 2 radius: 8 } //设置标题栏区域为拖拽区域 Text { width: parent.width height: 40 anchors.top: parent.top text: qsTr("标题栏") horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter MouseArea { property point clickPoint: "0,0" anchors.fill: parent acceptedButtons: Qt.LeftButton onPressed: { clickPoint = Qt.point(mouse.x, mouse.y) } onPositionChanged: { var offset = Qt.point(mouse.x - clickPoint.x, mouse.y - clickPoint.y) setDlgPoint(offset.x, offset.y) } } } Label { x: 189 y: 128 width: 171 height: 15 text: qsTr("AAAAAAAAAAAAAAAAAAA") } Button { x: 103 y: 204 text: "ok" onClicked: { root.close() } } Button { x: 341 y: 204 text: qsTr("cancel") onClicked: { root.close() } } } function setDlgPoint(dlgX ,dlgY) { //设置窗口拖拽不能超过父窗口 if(root.x + dlgX < 0) { root.x = 0 } else if(root.x + dlgX > root.parent.width - root.width) { root.x = root.parent.width - root.width } else { root.x = root.x + dlgX } if(root.y + dlgY < 0) { root.y = 0 } else if(root.y + dlgY > root.parent.height - root.height) { root.y = root.parent.height - root.height } else { root.y = root.y + dlgY } } }
相关文章推荐
- WPF.UIShell UIFramework之自定义窗口的深度技术 - 模态闪动(Blink)、窗口四边拖拽支持(WmNCHitTest)、自定义最大化位置和大小(WmGetMinMaxInfo)
- 很好用的模态弹出窗口,可自定义弹出窗口外观
- HTML页面居中弹出自定义窗口层(实现可拖拽)
- 为bootstrap模态窗口添加拖拽(draggable )事件
- JQuery Dialog(JS 模态窗口,可拖拽的DIV)
- JQuery Dialog(JS模态窗口,可拖拽的DIV)
- Swift-打开其它Storyboard中的自定义模态窗口
- 综合应用WPF/WCF/WF/LINQ之三十四:在XBAP项目中模拟弹出一个自定义的“模态窗口”
- 自定义jquery模态窗口插件无法在顶层窗口显示问题
- Qt自定义样式/无边框窗口/子窗口模态/非模态问题
- JQuery Dialog(JS模态窗口,可拖拽的DIV)
- iPhone手机解锁效果&&自定义滚动条&&拖拽--Clone&&窗口拖拽(改变大小/最小化/最大化/还原/关闭)
- SilverLight中弹出自定义模态窗口
- 点击按钮弹出可自定义小弹框即实现模态窗口
- QML实现窗口拖拽与阴影
- QML之窗口(无边框、透明及拖拽)
- JQuery Dialog(JS 模态窗口,可拖拽的DIV)
- Jquery实现自定义窗口随意的拖拽
- Jquery实现自定义窗口随意的拖拽
- MFC模态对话框和非模态对话框及副窗口调用主窗口控件 自定义消息发送