未捕获对话框的接受/拒绝信号

时间:2018-07-13 14:22:25

标签: qt qml qt5 qtquickcontrols2

将Qt升级到v5.10.1,对话框不会发出接受/拒绝信号。可以正常工作的最后一个已知版本是5.10.0。我的问题是-我会错过某些东西还是使用了错误的方式使用某些组件?还是回归(我会报告?)

问题已在macOS 10.12+和Win10(VC 2015/2017)上复制

简化的源代码示例:

CMakeLists.txt

cmake_minimum_required(VERSION 3.11)
project(bug-test LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Core Quick REQUIRED)
add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc")
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Quick)

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty()) return -1;
    return app.exec();
}

main.qml

import QtQuick 2.9
import QtQuick.Controls 2.2

ApplicationWindow {
    id: applicationWindow
    visible: true
    Button {
        text: qsTr("Push me!")
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        onClicked: dlgLoader.source = "qrc:/MsgDialog.qml"
    }
    Loader {
        id: dlgLoader
        onStatusChanged: {
            if (dlgLoader.status == Loader.Ready) {
                item.parent = ApplicationWindow.overlay
                item.open()
            }
        }
    }
    Connections {
        target: dlgLoader.item
        onClosed: dlgLoader.source = ""
    }
}

MsgDialog.qml

import QtQuick 2.9

MsgDialogForm {
    x: (parent.width - width) / 2
    y: (parent.height - height) / 2
    width: Math.min(applicationWindow.width, applicationWindow.height) / 5 * 4

    onAccepted: console.log("Ok clicked; save answer")
    onRejected: console.log("Cancel clicked; don't save")
}

MsgDialogForm.ui.qml

import QtQuick 2.9
import QtQuick.Controls 2.3

Dialog {
    id: dialog
    modal: true
    standardButtons: Dialog.Yes | Dialog.No
    closePolicy: Popup.CloseOnEscape
}

qml.qrc

<RCC>
    <qresource prefix="/">
        <file>main.qml</file>
        <file>qtquickcontrols2.conf</file>
        <file>MsgDialog.qml</file>
        <file>MsgDialogForm.ui.qml</file>
    </qresource>
</RCC>

只需启动应用程序;按下按钮,然后单击对话框按钮之一。 QtCreator控制台中没有显示任何内容,但对话框已关闭。

1 个答案:

答案 0 :(得分:1)

Loader中建立新项目时,将从内存中删除前一个项目,在您的情况下,您是在关闭窗口时执行此操作,但是在窗口关闭后会发出接受或拒绝的信号关闭。

一种可能的解决方案是创建一个在接收到消息后发出的信号。

MsgDialog.qml

import QtQuick 2.9

MsgDialogForm {
    signal finished()
    x: (parent.width - width) / 2
    y: (parent.height - height) / 2
    width: Math.min(applicationWindow.width, applicationWindow.height) / 5 * 4

    onAccepted: {
        console.log("Ok clicked; save answer")
        finished()
    }
    onRejected: {
        console.log("Cancel clicked; don't save")
        finished()
    }
}

main.qml

...
Connections {
    target: dlgLoader.item
    onFinished: dlgLoader.source = ""
}