错误 C2280:“QQmlPrivate::QQmlElement<T>::QQmlElement(void)":尝试引用已删除的函数 [英] error C2280: &#39;QQmlPrivate::QQmlElement&lt;T&gt;::QQmlElement(void)&#39;: attempting to reference a deleted function

查看:77
本文介绍了错误 C2280:“QQmlPrivate::QQmlElement<T>::QQmlElement(void)":尝试引用已删除的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Qt\Examples\Qt-5.9\quick\views 中操作 qt 项目的一部分,我是 qml 的新手,我正在尝试每次打开一个不同的 QDialog 窗口,具体取决于已单击的 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("Interfacage", 1, 0,"Component:MouseArea");QQmlApplicationEngine 引擎;engine.load(QUrl(QStringLiteral("qrc:/main.qml")));返回 app.exec();}

这里是interfacageqml.h:

#ifndef INTERFACAGEQML_H#define INTERFACAGEQML_H#include #include "qtinterface.h"类接口QML:公共QObject{Q_OBJECT上市:interfaceQML(QObject *parent);~接口QML();Q_INVOKABLE void mouseClick();信号:无效点击();};#endif//INTERFACAGEQML_H

interfacageqml.cpp:

#include "interfacageqml.h"#include #include interfaceQML::interfacageQML(QObject *parent):QObject(父){}interfaceQML::~interfacageQML(){}void interfaceQML::mouseClick(){qDebug() <<"qmlinterface::mouseClick()";发出 clicked();}

我的项目是这样组织的:

qmlinterface.qrc 文件包含以下路径:

ma​​in.qml :

导入QtQuick 2.6导入 QtQuick.Window 2.2窗户 {可见:真实宽度:640高度:480标题:qsTr("Hello World")主窗体{anchors.fill:父级}}

MainForm.qml :

导入QtQuick 2.6将 QtQuick.Controls 2.0 导入为 QQC2导入接口 1.0长方形 {宽度:800高度:800白颜色"列表模型{id:应用程序模型列表元素{名称:联系人"图标:图片/资源/AddressBook_48.png"}列表元素{名称:《音乐》图标:图片/资源/AudioPlayer_48.png"}列表元素{名称:《电影》图标:图片/资源/VideoPlayer_48.png"}列表元素{名称:相机"图标:图片/资源/Camera_48.png"}列表元素{名称:日历"图标:图片/资源/DateBook_48.png"}列表元素{名称:待办事项清单"图标:图片/资源/TodoList_48.png"}}成分 {id:appDelegate物品 {宽度:100高度:100比例尺:PathView.iconScale图像 {id:我的图标y:20anchors.horizo​​ntalCenter: parent.horizo​​ntalCenter来源:图标}文本 {锚点{顶部:myIcon.bottom水平中心:parent.horizo​​ntalCenter}文字:姓名}鼠标区域{anchors.fill:父级已点击:{view.currentIndex = 索引接口.mouseClick}}}}成分 {id:appHighlight长方形 {宽度:100身高:80颜色:轻钢蓝"}}路径视图{编号:查看anchors.fill:父级亮点:应用亮点首选高光开始:0.5优选高光结束:0.5焦点:真实型号:appModel委托:appDelegate路径:路径{开始X:50起始时间:80路径属性{名称:iconScale"值:2.0}路径四{×:250y:200控制X:50控制:200}路径属性{名称:iconScale"值:2.0}路径四{×:600y:50控制X:400控制:200}路径属性{名称:iconScale"值:2.0}}}}

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

<块引用>

错误:C2280

但是,当我评论这一行时:qmlRegisterType("Interfacage", 1, 0, "Component:MouseArea"); 项目运行,我可以在路径视图组件之间导航主窗体.

解决方案

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

qmlRegisterType("Interfacage", 1, 0, "InterfacageQML");

另一个错误是默认情况下您必须传递父项,在这种情况下为 0 或 nullptr,因为项目可能没有父项.

class interfaceQML : public QObject{Q_OBJECT上市:显式接口QML(QObject *parent = nullptr);[...]

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

导入QtQuick 2.6将 QtQuick.Controls 2.0 导入为 QQC2导入接口 1.0长方形 {宽度:800高度:800白颜色"接口QML{编号:我的项目}[...]

最后如果你想使用它,你必须通过 item 调用函数.

MouseArea {anchors.fill:父级已点击:{view.currentIndex = 索引myitem.mouseClick()}}

<小时>

由于您想通过该类将 QDialogQML 连接起来,因此您不能这样做,因为它们将是不同的对象,因此一种解决方案是使用单例,为此您必须执行以下操作:

interfacageqml.h

#ifndef INTERFACAGEQML_H#define INTERFACAGEQML_H#include #include 类接口QML:公共QObject{Q_OBJECT静态接口QML*实例;显式接口QML(QObject *parent = nullptr);上市:静态接口QML *getInstance();~接口QML();Q_INVOKABLE void mouseClick();信号:无效点击();};#endif//INTERFACAGEQML_H

interfacageqml.cpp

#include "interfacageqml.h"#include interfaceQML* interfaceQML::instance = 0;interfaceQML *interfacageQML::getInstance(){如果(实例 == 0)实例 = 新的接口 QML;返回实例;}interfaceQML::interfacageQML(QObject *parent) : QObject(parent){}interfaceQML::~interfacageQML(){}void interfaceQML::mouseClick(){qDebug() <<"qmlinterface::mouseClick()";发出 clicked();}

ma​​in.cpp

#include "interfacageqml.h"#include #include 静态 QObject *singletonTypeProvider(QQmlEngine *, QJSEngine *){返回接口QML::getInstance();}int main(int argc, char *argv[]){qmlRegisterSingletonType("Interfacage", 1, 0, "InterfacageQML", singletonTypeProvider);//测试interfaceQML *obj = qobject_cast(interfacageQML::getInstance());QObject::connect(obj, &interfaceQML::clicked,[]{qDebug()<<"点击";});QQmlApplicationEngine 引擎;engine.load(QUrl(QStringLiteral("qrc:/main.qml")));如果 (engine.rootObjects().isEmpty())返回-1;返回 app.exec();}

由于是单例,所以不需要创建项目,可以直接创建:

导入接口1.0[...]鼠标区域{anchors.fill:父级已点击:{view.currentIndex = 索引接口QML.mouseClick()}}

最后一个示例可以在以下链接中找到.>

I tried to operate a part of a qt project in Qt\Examples\Qt-5.9\quick\views, I am new to qml and I am trying to open each time a different QDialog window depending on qml pathview component that has been clicked. First of all, I started with creating a class (interfacageQML) which will serve to interface the qml Mainform and the QDialog (qtinterface), the necessary files are included among which interfacageqml.h.

here is the 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();                                                          
}

And here's 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();
}

My project is organised this way :

the qmlinterface.qrc file contains these paths:

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
            }
        }
    }
}

When I run this project, i got an error :

error:C2280

However, when I comment this line : qmlRegisterType<interfacageQML>("Interfacage", 1, 0, "Component:MouseArea"); the project runs and I can navigate between the pathview components in the MainForm.

解决方案

When you use qmlRegisterType you are registering a new data type in QML, it is not an object, in that case the name "Component: MouseArea" is not suitable.

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

Another error is that you must pass a parent by default, in this case 0 or nullptr since the items may not have parents.

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

As I said in the first lines, this is a new type, it is not an object so you must create it.

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
    }
    [...]

And in the end if you want to use it you must call the function through the item.

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


Since you want to connect your QDialog with the QML through that class, you can not do it since they will be different objects, one solution for this is to use a singleton, for this you must do the following:

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();
}

As it is a singleton it is not necessary to create an item, you can do it directly:

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

This last example can be found in the following link.

这篇关于错误 C2280:“QQmlPrivate::QQmlElement<T>::QQmlElement(void)":尝试引用已删除的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆