zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    分布式事务之最终一致性BASE理论
    CAP理论
    Comparator中返回0导致数据丢失的大坑
    电脑主板分类
    SimpleDateFormat线程不安全
    Redis面试题
    JS闭包
    ES6将两个数组合并成一个对象数组
    视频色彩空间RGB、YUV、YCbCr
    c#接口作用的深入理解
  • 原文地址:https://www.cnblogs.com/BASE64/p/14473605.html
Copyright © 2011-2022 走看看