MyDialog.qml内容如下:
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2
Window {
id: root
width: 300
height: 100
flags: Qt.Popup
modality: Qt.ApplicationModal
x: Screen.width/2-width/2
y: Screen.height/2-height/2
Column{
anchors.fill: parent
Rectangle{
width: parent.width
height: 30
color: "silver"
Text{
width: contentWidth
height: parent.height
text: "信息提示"
}
Button{
anchors.right: parent.right
height: parent.height
width: height
}
}
Rectangle{
width: parent.width
height: 200
color: "green"
Text{
width: parent.width
anchors.centerIn: parent
text: "PPPP"
font.pixelSize: 27
focus: true
wrapMode: Text.Wrap
}
}
Rectangle{
width: parent.width
height: 30
color: "silver"
Button{
DebugRect{}
id: btnOk
focus: true
width: 100
height: parent.height
anchors.centerIn: parent
onClicked: {
root.hide();
}
Keys.onReturnPressed: {
root.hide();
}
}
}
}
onActiveChanged: {
if(active == true)
{
btnOk.forceActiveFocus();
}
}
}
main.qml中打开对话框的代码如下:
Button{
DebugRect{}
width: 100
height: 50
focus: true
activeFocusOnPress: true
Keys.onReturnPressed: {
dialog.show();
dialog.requestActivate();
}
}
MyDialog{
id: dialog
width: 500
height: 400
visible: false
}