Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Qt Quick应用程序无法通过Qt.createComponent方法动态创建控件_Qt_Qml_Qtquick2 - Fatal编程技术网

为什么Qt Quick应用程序无法通过Qt.createComponent方法动态创建控件

为什么Qt Quick应用程序无法通过Qt.createComponent方法动态创建控件,qt,qml,qtquick2,Qt,Qml,Qtquick2,在我的Qt Quick应用程序中,我想使用Qt.createComponent动态创建组件——我期望的效果是:当我单击buttoncreate复选框时,窗口将动态创建一个复选框 我在一个Qt Quick UI prototypeqml-only项目中实现了它,但在Qt Quick应用程序中失败了 运行应用程序后,我多次单击buttoncreate复选框,但窗口没有响应,程序卡住 // app.pro QT += quick CONFIG += c++11 # The following de

在我的Qt Quick应用程序中,我想使用Qt.createComponent动态创建组件——我期望的效果是:当我单击buttoncreate复选框时,窗口将动态创建一个复选框

我在一个Qt Quick UI prototypeqml-only项目中实现了它,但在Qt Quick应用程序中失败了

运行应用程序后,我多次单击buttoncreate复选框,但窗口没有响应,程序卡住

// app.pro
QT += quick

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Refer to the documentation for the
# deprecated API to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target



//main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine> 
#include <QQmlComponent>
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

// CheckBox.qml
import QtQuick 2.0
import QtQuick.Controls 2.12
CheckBox{
    text: "checkcheck"

}

//main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    id:root
    visible: true
    width: 200
    height: 200
    title: qsTr("Hello World")

    Button{
        text: "create checkbox"
        anchors.centerIn:parent
        onClicked: loadCheckBox(root)
    }

    function loadCheckBox(parent){
        var obj=Qt.createComponent("CheckBox.qml");
        if(obj.status==Component.Ready){
            var checkBox=obj.createObject(parent)
        }

    }
}
我的环境:Win10 Qt5.13.2_MSVC2017_64位


有人知道如何修改吗?

只需将CheckBox.qml重命名为MyCheckBox.qml或类似名称。您应该避免以与现有组件相同的方式命名自定义组件。打印错误字符串应有助于:@folibis非常感谢!!!!我解决了问题!!!!:只需将CheckBox.qml重命名为MyCheckBox.qml或类似名称。您应该避免以与现有组件相同的方式命名自定义组件。打印错误字符串应有助于:@folibis非常感谢!!!!我解决了问题!!!!: