• QML出现Signal QQmlEngine::quit() emitted, but no receivers connected to handle it.


    两个文件的代码如下,实现的功能很简单:点击Rectangle窗口中的Quit按钮,窗口关闭

    //main.cpp
    #include <QGuiApplication>
    #include <QQuickView>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQuickView viewer;
        viewer.setResizeMode(QQuickView::SizeRootObjectToView);
        viewer.setSource(QUrl("qrc:///main.qml"));
        viewer.show();
    
        return app.exec();
    }
    //main.qml
    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.14
    
    //Button的clicked事件响应onClicked
    
    Rectangle {
         640; 
         height: 480;
        color: "gray";
    
        Button {
            text: "Quit";
            anchors.centerIn: parent;
            onClicked: {
                Qt.quit();
            }
        }
    }

    运行提示:Signal QQmlEngine::quit() emitted, but no receivers connected to handle it.

    解决方法:如果使用的是Rectangle,在main.cpp中include QQmlEngine库,再加上

    QObject::connect(viewer.engine(), SIGNAL(quit()), &app, SLOT(quit()));即可。&app也可以用qApp替代。qApp是一个宏。

    官方帮助文档
    This function causes the QQmlEngine::quit() signal to be emitted. Within the Prototyping with qmlscene, this causes the launcher application to exit; to quit a C++ application when this method is called, connect the QQmlEngine::quit() signal to the QCoreApplication::quit() slot.

    //main.cpp
    #include <QGuiApplication>
    #include <QQuickView>
    #include <QQmlEngine>
    #include <QObject>//<----------------------------------------------------------------新添加的——需要包含头文件
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQuickView viewer;
        viewer.setResizeMode(QQuickView::SizeRootObjectToView);
        viewer.setSource(QUrl("qrc:///main.qml"));
        QObject::connect(viewer.engine(), SIGNAL(quit()), &app, SLOT(quit()));//<----------------------新添加的
        viewer.show();
    
        return app.exec();
    }
    //main.qml
    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.14
    import QtQml 2.14//<-----------------------------------------------------------------------------新添加的
    
    //Button的clicked事件响应onClicked
    
    Rectangle {
         640;
         height: 480;
        color: "gray";
    
        Button {
            text: "Quit";
            anchors.centerIn: parent;
            onClicked: {
                Qt.quit();
            }
        }
    }
  • 相关阅读:
    连接SDE数据库hl3292修改已解决
    ORACLE 10g 安装
    SharpMap相关的一些博客和网站
    Delphi 学习之函数 ②扩展的日期时间操作函数
    Delphi学习之函数 ⑾进制函数及过程
    Delphi学习之函数 ① 扩展的字符串操作函数
    html 初次接触html学习【1】
    Delphi 学习之函数 ③ 扩展的位操作函数
    Delphi学习之函数 ⑨汉字拼音功能函数
    Delphi学习之函数 ④扩展的文件及目录操作函数
  • 原文地址:https://www.cnblogs.com/BASE64/p/14473605.html
Copyright © 2020-2023  润新知