我在尝试从ApplicationWindow切换到另一个时遇到了这个问题。
我有第一个名为login.qml的Window,它通过以下信号打开main.qml:
onIdAutenticadoChanged: {
if(idAutenticado > 0){
console.log("login.qml: Autenticado ID: " + idAutenticado);
LoginController.abrirMain();
close();
} else if(idAutenticado == 0){
senhaInput.text = "";
console.log("Falha na autenticação: Usuário e/ou senha inválidos.");
lblMsgErro.text = "Usuário e/ou senha inválidos.";
lblMsgErro.visible = true;
loginInput.focus = true;
}
}
插槽 LoginController.abrirMain()将加载并显示main.qml
void QLoginController::abrirMain()
{
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("MainController", new QMainController(0,m_autenticado));
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
QQuickWindow* window = qobject_cast<QQuickWindow*>(engine.rootObjects().at(0));
window->showFullScreen();
}
问题是:没有显示main.qml。
//main.qml
ApplicationWindow {
id: mainWindow
visible: true
modality: "WindowModal"
visibility: "FullScreen"
color: "#09435b"
}
问题:我是否使用正确的方法打开这个新窗口?如果没有,我该怎么做?
答案 0 :(得分:1)
因为您在堆栈上创建了引擎:),它将在abrirMain()
返回之前自动删除,因此也会破坏窗口。
你应该在堆上创建引擎。像这样:
// 'this' will become engine's parent and will automaticlaly delete engine when 'this' is deleted
QQmlApplicationEngine *engine = new QQmlApplicationEngine(this);
engine->rootContext()->setContextProperty("MainController", new QMainController(0,m_autenticado));
engine->load(QUrl(QStringLiteral("qrc:///main.qml")));
QQuickWindow* window = qobject_cast<QQuickWindow*>(engine->rootObjects().at(0));
window->showFullScreen();