gpt4 book ai didi

qt - 错误 C2280 : 'QQmlPrivate::QQmlElement::QQmlElement(void)' : attempting to reference a deleted function

转载 作者:行者123 更新时间:2023-12-04 13:22:44 27 4
gpt4 key购买 nike

我试图在Qt\Examples\Qt-5.9\quick\views中操作一个qt项目的一部分, 我是新来的 qml我每次都试图打开不同的 QDialog窗口取决于 qml已单击的路径 View 组件。首先,我开始创建一个 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();
}

我的项目是这样组织的:

enter image description here

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

enter image description here

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");项目运行,我可以在 MainForm 中的路径 View 组件之间导航。

最佳答案

当您使用 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
}
[...]

最后,如果你想使用它,你必须通过 item 调用该函数。
MouseArea {
anchors.fill: parent
onClicked: {
view.currentIndex = index
myitem.mouseClick()
}
}

既然你想连接你的 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();
}

由于它是单例,因此无需创建项目,您可以直接进行:
import Interfacage 1.0
[...]
MouseArea {
anchors.fill: parent
onClicked: {
view.currentIndex = index
InterfacageQML.mouseClick()
}
}

最后一个例子可以在以下 link 中找到.

关于qt - 错误 C2280 : 'QQmlPrivate::QQmlElement<T>::QQmlElement(void)' : attempting to reference a deleted function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47715491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com