错误 C2280:'QQmlPrivate::QQmlElement<T>::QQmlElement(void)':试图引用已删除的函数

error C2280: 'QQmlPrivate::QQmlElement<T>::QQmlElement(void)': attempting to reference a deleted function

我试图在 Qt\Examples\Qt-5.9\quick\views 中操作一个 qt 项目的一部分,我是 qml 的新手,每次我都试图打开不同的 QDialog window 取决于 qml 已单击的路径视图组件。首先,我开始创建一个 class (interfacageQML),它将用于连接 qml Mainform 和 QDialog (qtinterface),必要的其中包含文件 interfacageqml.h.

这里是 main.cpp :

#include "interfacageqml.h"                                               

int main(int argc, char *argv[]) 
{                                                             
    QGuiApplication app(argc, argv);             
    qmlRegisterType<interfacageQML>("Interfacage", 1, 0,"Component:MouseArea");

   QQmlApplicationEngine engine;
   engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

   return app.exec();                                                          
}

这里是 interfacageqml.h :

#ifndef INTERFACAGEQML_H
#define INTERFACAGEQML_H

#include <QObject>
#include "qtinterface.h"

class interfacageQML : public QObject
{
Q_OBJECT

public:
interfacageQML(QObject *parent);
~interfacageQML();


Q_INVOKABLE void mouseClick();

signals:
    void clicked();

};

#endif // INTERFACAGEQML_H

interfacageqml.cpp :

#include "interfacageqml.h"
#include <QDebug>
#include <QApplication>

interfacageQML::interfacageQML(QObject *parent)
    : QObject(parent)
{

}

interfacageQML::~interfacageQML()
{

}

void interfacageQML::mouseClick()
{
    qDebug() << "qmlinterface::mouseClick()";
    emit clicked();
}

我的项目是这样组织的:

qmlinterface.qrc 文件包含这些路径:

main.qml :

import QtQuick 2.6
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    MainForm{
        anchors.fill: parent
    }
}

MainForm.qml :

import QtQuick 2.6
import QtQuick.Controls 2.0 as QQC2
import Interfacage 1.0


Rectangle {
    width: 800
    height: 800
    color: "white"

    ListModel {
        id: appModel
        ListElement {
            name: "Contacts"
            icon: "pics/Resources/AddressBook_48.png"
        }
        ListElement {
            name: "Music"
            icon: "pics/Resources/AudioPlayer_48.png"
        }
        ListElement {
            name: "Movies"
            icon: "pics/Resources/VideoPlayer_48.png"
        }
        ListElement {
            name: "Camera"
            icon: "pics/Resources/Camera_48.png"
        }
        ListElement {
            name: "Calendar"
            icon: "pics/Resources/DateBook_48.png"
        }
        ListElement {
            name: "Todo List"
            icon: "pics/Resources/TodoList_48.png"
        }
    }

    Component {
        id: appDelegate
        Item {
            width: 100
            height: 100
            scale: PathView.iconScale

            Image {
                id: myIcon
                y: 20
                anchors.horizontalCenter: parent.horizontalCenter
                source: icon
            }
            Text {
                anchors {
                    top: myIcon.bottom
                    horizontalCenter: parent.horizontalCenter
                }
                text: name
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    view.currentIndex = index
                    Interfacage.mouseClick
                }
            }
        }
    }

    Component {
        id: appHighlight
        Rectangle {
            width: 100
            height: 80
            color: "lightsteelblue"
        }
    }

    PathView {
        id: view
        anchors.fill: parent
        highlight: appHighlight
        preferredHighlightBegin: 0.5
        preferredHighlightEnd: 0.5
        focus: true
        model: appModel
        delegate: appDelegate
        path: Path {
            startX: 50
            startY: 80
            PathAttribute {
                name: "iconScale"
                value: 2.0
            }
            PathQuad {
                x: 250
                y: 200
                controlX: 50
                controlY: 200
            }
            PathAttribute {
                name: "iconScale"
                value: 2.0
            }
            PathQuad {
                x: 600
                y: 50
                controlX: 400
                controlY: 200
            }
            PathAttribute {
                name: "iconScale"
                value: 2.0
            }
        }
    }
}

当我 运行 这个项目时,我得到了一个 error :

error:C2280

但是,当我评论这一行时:qmlRegisterType<interfacageQML>("Interfacage", 1, 0, "Component:MouseArea"); 项目 运行s 并且我可以在 MainForm 中的路径视图组件之间导航。

当您使用 qmlRegisterType 时,您在 QML 中注册了一个新的数据类型,它不是一个对象,在这种情况下名称 "Component: MouseArea" 不合适。

qmlRegisterType<interfacageQML>("Interfacage", 1, 0, "InterfacageQML");

另一个错误是您必须默认传递父项,在本例中为 0 或 nullptr,因为项目可能没有父项。

class interfacageQML : public QObject
{
    Q_OBJECT
public:
    explicit interfacageQML(QObject *parent = nullptr);
    [...]

正如我在第一行所说,这是一个新类型,它不是一个对象,因此您必须创建它。

import QtQuick 2.6
import QtQuick.Controls 2.0 as QQC2
import Interfacage 1.0

Rectangle {
    width: 800
    height: 800
    color: "white"

    InterfacageQML{
        id: myitem
    }
    [...]

最后,如果你想使用它,你必须通过项目调用函数。

MouseArea {
    anchors.fill: parent
    onClicked: {
        view.currentIndex = index
        myitem.mouseClick()
    }
}

因为你想通过 class 连接你的 QDialogQML,你不能这样做,因为它们是不同的对象,一个解决方案是使用单身人士,为此您必须执行以下操作:

interfacageqml.h

#ifndef INTERFACAGEQML_H
#define INTERFACAGEQML_H

#include <QObject>
#include <QQmlEngine>    

class interfacageQML : public QObject
{
    Q_OBJECT
    static interfacageQML* instance;
    explicit interfacageQML(QObject *parent = nullptr);
public:
    static interfacageQML *getInstance();
    ~interfacageQML();
    Q_INVOKABLE void mouseClick();

signals:
    void clicked();
};
#endif // INTERFACAGEQML_H

interfacageqml.cpp

#include "interfacageqml.h"
#include <QDebug>

interfacageQML* interfacageQML::instance = 0;

interfacageQML *interfacageQML::getInstance()
{
    if (instance == 0)
        instance = new interfacageQML;
    return instance;
}

interfacageQML::interfacageQML(QObject *parent) : QObject(parent)
{
}

interfacageQML::~interfacageQML()
{
}

void interfacageQML::mouseClick()
{
    qDebug() << "qmlinterface::mouseClick()";
    emit clicked();
}

main.cpp

#include "interfacageqml.h"

#include <QGuiApplication>
#include <QQmlApplicationEngine>

static QObject *singletonTypeProvider(QQmlEngine *, QJSEngine *)
{

    return interfacageQML::getInstance();
}


int main(int argc, char *argv[])
{
    qmlRegisterSingletonType<interfacageQML>("Interfacage", 1, 0, "InterfacageQML", singletonTypeProvider);

    // test
    interfacageQML *obj = qobject_cast<interfacageQML*>(interfacageQML::getInstance());
    QObject::connect(obj, &interfacageQML::clicked,[]{
        qDebug()<<"clicked";
    });

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

因为是单例所以不需要创建item,可以直接创建:

import Interfacage 1.0
[...]
MouseArea {
    anchors.fill: parent
    onClicked: {
        view.currentIndex = index
        InterfacageQML.mouseClick()
    }
}

最后一个示例可以在以下 link.

中找到