接受/拒绝未捕获的对话信号
Posted
技术标签:
【中文标题】接受/拒绝未捕获的对话信号【英文标题】:Accept/reject signals of dialog not catched 【发布时间】:2018-07-13 14:22:25 【问题描述】:将 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】:当在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 = ""
【讨论】:
以上是关于接受/拒绝未捕获的对话信号的主要内容,如果未能解决你的问题,请参考以下文章
iframe chrome:未捕获的 DOMException:无法从“Window”读取“localStorage”属性:此文档的访问被拒绝