简体   繁体   English

QT / QML从主窗口重新打开新窗口

[英]QT/QML reopen New Window From Main Window

I started working on QT/QML, PyQt Desktop Application. 我开始研究QT / QML,PyQt桌面应用程序。 I have some basic knowledge of Qt/QML and all. 我有一些Qt / QML的基本知识。

I'm trying to modify one Desktop Application already Exists. 我正在尝试修改已存在的一个桌面应用程序。

Here is I've add one Button in Main Window of Application which Open another Window. 这是我在应用程序的主窗口中添加一个按钮打开另一个窗口。

main.qml main.qml

Button {
    text: "Open Window"

    Loader{ id: pageLoader }

    onClicked: {
        console.log("Clicked")
        pageLoader.source = "testing.qml"
    }
}

testing.qml testing.qml

import QtQuick 2.2
import QtQuick.Window 2.2

Window {
    id: win1
    width: 1000;
    height: 1000;
    visible: true;
    visibility: "Maximized"
    color: "#363636";
    title: "First Window";
    Text {
        anchors.centerIn: parent
        text: "Page 1"
    }
    MouseArea{
        anchors.fill: parent;
        onClicked: pageLoader.source="";
    }
}

When I clicked on Button, New Window is open Successfully. 当我点击按钮时,新窗口成功打开。 But When I closed new Window and Tried to reopen, It'll not open. 但当我关闭新窗口并尝试重新打开时,它将无法打开。 It just print "Clicked". 它只是打印“点击”。

Your problem is, that you clear the Loader s source, only when you close the window by clicking on the MouseArea it contains. 您的问题是,只有当您通过单击它包含的MouseArea关闭窗口时才清除Loader的源。 If you close it with the x -button you don't clear it, so the loader stays active and the source stays the same. 如果用x按钮关闭它,则不清除它,因此加载器保持活动状态且源保持不变。

To solve this, you can either make sure, the source changes, when ever you click the open- Button , by having 要解决这个问题,您可以确定,更改源,何时单击打开Button ,通过

onClicked: {
    pageLoader.source = ""  // Clearing first
    pageLoader.source = "testing.qml" // Setting again
}

or by using the Window s closing() -signal 或者使用Window s closing() - 信号

onClosing: pageLoader.source = ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM