Ľubomír Carik
Ľubomír Carik

Reputation: 407

Accept/reject signals of dialog not catched

Upgrading Qt to v5.10.1 the Dialog doesn't emit accept/reject signals. The last known version that works smooth was 5.10.0. My question is - does I miss something or I use some component in wrong way? Or it is regression (and I will report it?)

Issue reproduced on macOS 10.12+ & Win10 (VC 2015/2017)

Simplified source code sample:

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>

Just start the app; push the button and click one of dialog buttons. Nothing is shown in QtCreator console but dialog is closed.

Upvotes: 0

Views: 549

Answers (1)

eyllanesc
eyllanesc

Reputation: 244301

When a new item is established in the Loader the previous item is deleted from the memory, in your case you are doing it when the window is closed, but the accepted or rejected signal is emited after the window is closed.

A possible solution is to create a signal that is emited after receiving the message.

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 = ""
}

Upvotes: 1

Related Questions