注意
1.插件式调用的话不需要事先在主窗口里定义子窗口。
2.插件式调用的话会存在重复调用子窗口方法,如果仅希望出现一个子窗口请不要使用此方法。
Main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
id : mainWindow //必须定义主窗口Id
visible: true
width: 640
height: 480
title: qsTr("Hello World")
property variant win //必须定义一个变量
Rectangle {
width: 320; height: 240
color: "black"
Text {
id: txt
text: "打开另一个窗口"
font.pixelSize: 20
anchors.centerIn: parent
}
MouseArea {
id: mouse_area
anchors.fill: parent // 有效区域
onClicked: {
var c = Qt.createComponent("Qml1.qml")
win = c.createObject(mainWindow)
win.show()
}
}
}
}
Qml1.qml
import QtQuick 2.0
import QtQuick.Window 2.2
Window {
width: 320; height: 240
visible: false
color: "lightblue"
Text {
id: txt
text: "另一个窗口"
font.pixelSize: 20
anchors.centerIn: parent
}
}
python显示窗口
from PyQt5.QtQuick import QQuickView
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtQml,QtQuick
import sys
app = QtWidgets.QApplication(sys.argv)
engine = QtQml.QQmlApplicationEngine(QUrl('main.qml')) # 显示window界面
sys.exit(app.exec_())