错误 C2280: 'QQmlPrivate::QQmlElement<T>::QQmlElement(void)': 试图引用已删除的函数 [英] error C2280: 'QQmlPrivate::QQmlElement<T>::QQmlElement(void)': attempting to reference a deleted function
问题描述
我尝试在 QtExamplesQt-5.9quickviews
中操作 qt 项目的一部分,我是 qml
的新手,我正在尝试每次打开不同的 QDialog
窗口,具体取决于已单击的 qml
pathview 组件.首先,我开始创建一个 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 <QObject>#include qtinterface.h"类接口QML:公共QObject{Q_OBJECT上市:接口QML(QObject *parent);〜接口QML();Q_INVOKABLE void mouseClick();信号:无效点击();};#endif//INTERFACAGEQML_H
interfacageqml.cpp:
#include "interfacageqml.h"#include <QDebug>#include <QApplication>interfaceQML::interfacageQML(QObject *parent):QObject(父){}接口QML::~接口QML(){}无效接口QML::mouseClick(){qDebug() <<"qmlinterface::mouseClick()";发出 clicked();}
我的项目是这样组织的:
qmlinterface.qrc 文件包含以下路径:
main.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图片 {编号:我的图标是:20anchors.horizontalCenter: 父级.horizontalCenter来源:图标}文本 {锚{顶部:myIcon.bottom水平中心:parent.horizontalCenter}文字:姓名}鼠标区 {anchors.fill:父级已点击:{view.currentIndex = 索引界面.mouseClick}}}}零件 {id: 应用高亮长方形 {宽度:100身高:80颜色:轻钢蓝"}}路径视图 {编号:查看anchors.fill:父级亮点:appHighlight首选高光开始:0.5首选高亮结束:0.5重点:真型号:appModel委托人:appDelegate路径:路径{开始X:50开始:80路径属性 {名称:图标比例"值:2.0}路径四{×:250y: 200控制X:50控制Y:200}路径属性 {名称:图标比例"值:2.0}路径四{×:600是:50控制X:400控制Y:200}路径属性 {名称:图标比例"值:2.0}}}}
当我运行这个项目时,我得到了一个错误
:
错误:C2280
但是,当我评论这一行时: qmlRegisterType
项目运行,我可以在路径视图组件之间导航主窗体.
当你使用 qmlRegisterType 在 QML 中注册一个新的数据类型时,它不是一个对象,在这种情况下名称Component: MouseArea"不合适.
qmlRegisterType("Interfacage", 1, 0, "InterfacageQML");
另一个错误是默认情况下您必须传递父级,在本例中为 0 或 nullptr,因为项目可能没有父级.
类接口QML:公共QObject{Q_OBJECT上市:显式接口QML(QObject *parent = nullptr);[...]
正如我在第一行所说的,这是一个新类型,它不是一个对象,所以你必须创建它.
导入QtQuick 2.6将 QtQuick.Controls 2.0 导入为 QQC2导入接口 1.0长方形 {宽度:800身高:800白颜色"接口QML{编号:myitem}[...]
最后如果要使用它必须通过item调用函数.
MouseArea {anchors.fill:父级已点击:{view.currentIndex = 索引myitem.mouseClick()}}
<小时>
由于您想通过该类将 QDialog
与 QML
连接,因此您不能这样做,因为它们将是不同的对象,一种解决方案是使用单身人士,为此您必须执行以下操作:
interfacageqml.h
#ifndef INTERFACAGEQML_H#define INTERFACAGEQML_H#include <QObject>#include <QQmlEngine>类接口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 <QDebug>接口QML* 接口QML::instance = 0;接口QML *interfacageQML::getInstance(){如果(实例 == 0)实例=新接口QML;返回实例;}interfaceQML::interfacageQML(QObject *parent) : QObject(parent){}接口QML::~接口QML(){}无效接口QML::mouseClick(){qDebug() <<"qmlinterface::mouseClick()";发出 clicked();}
main.cpp
#include "interfacageqml.h"#include <QGuiApplication>#include <QQmlApplicationEngine>静态 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, &interfacageQML::clicked,[]{qDebug()<<"点击";});QQmlApplicationEngine 引擎;engine.load(QUrl(QStringLiteral("qrc:/main.qml")));if (engine.rootObjects().isEmpty())返回-1;返回 app.exec();}
因为是单例所以不需要创建item,直接创建即可:
导入接口 1.0[...]鼠标区 {anchors.fill:父级已点击:{view.currentIndex = 索引接口QML.mouseClick()}}
可以在以下链接中找到最后一个示例..p>
I tried to operate a part of a qt project in QtExamplesQt-5.9quickviews
, 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屋!