繁体   English   中英

为什么这不是“调用'QQmlElement'的隐式删除默认构造函数中的默认构造函数

[英]Why is this not a default constructor in "Call to implicitly-deleted default constructor of 'QQmlElement'

我正在尝试在 Qml 引擎中注册 JB_NodeModel。 我收到此错误:

“调用 QQmlElement(JB_NodeModel) 的隐式删除默认构造函数 - QQmlElement(JB_NodeModel) 的默认构造函数被隐式删除,因为基础 class JB_NodeModel 没有默认构造函数。”

但我看不出默认构造函数有问题。 任何帮助将不胜感激。

我使用了初始化列表,但似乎没有帮助。

这是 JB_NodeModel.h 的精简版:

class SP3CORESHARED_EXPORT JB_NodeModel : public QAbstractItemModel
{
    Q_OBJECT
public:
 explicit JB_NodeModel(QObject *parent = nullptr);

~JB_NodeModel();

protected:
    JB_Node* rootNode;
    JB_Node* actingRootNode;
    bool isValidURL(QString fieldName, JB_Node* node) const;

    QVector<QString> columnHeadingsV;    
    QHash<int, QVector<QString>> mapped_dbFieldNamesH;    
    QList<QString> unEditableDBFieldNamesList;
    QList<QString> editableCheckBoxDBFieldNamesList; 

private:
    JB_DatabaseManager& mDB;
    int jobID;
    int numOfLevels;
    QSharedPointer<JB_NodeModelHelpProt> nodeModelHelper;
    QVector<QSharedPointer<QHash<QString, JB_Node*>>> nodesHV;     
    QVector<JB_Node*> alteredNodesV;
    bool isTreeModel;
    bool canEmitDataChanged;

 };

这是 JB_NodeModel.cpp 构造函数源:

JB_NodeModel::JB_NodeModel(QObject *parent)
: QAbstractItemModel(parent),
  rootNode(nullptr),
  actingRootNode(nullptr),
  mDB(JB_DatabaseManager::instance()),
  jobID(aJobID),
  numOfLevels(1),
  nodeModelHelper(nullptr),
  isTreeModel(true),
  canEmitDataChanged(true),
  dragDropHelper(false)
{
    QSharedPointer<JB_NodeModelHelpProt> aNodeModelHelper(new JB_NodeModHelp_TreeGrpPers());
    nodeModelHelper = aNodeModelHelper;

    canEmitDataChanged = true;
    dragDropHelper = false;

    Q_ASSERT(!nodeModelHelper.isNull());

    alteredNodesV.clear();

    rootNode = nullptr;
    actingRootNode = nullptr;

等等

这里是在 QML 引擎中注册:

QGuiApplication app(argc, argv);

qmlRegisterType<JB_NodeModel>("JB_NodeModel", 1, 0, "JB_NodeModel");


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

这是 qqmlprivate.h 中围绕 QQmlElement 的相关部分:

class QJSEngine;
class QQmlEngine;
class QQmlCustomParser;
namespace QQmlPrivate
{
    void Q_QML_EXPORT qdeclarativeelement_destructor(QObject *);
    template<typename T>
    class QQmlElement final : public T
{
public:
    ~QQmlElement() override {
        QQmlPrivate::qdeclarativeelement_destructor(this);
    }
    static void operator delete(void *ptr) {
        // We allocate memory from this class in QQmlType::create
        // along with some additional memory.
        // So we override the operator delete in order to avoid the
        // sized operator delete to be called with a different size than
        // the size that was allocated.
        ::operator delete (ptr);
    }
    static void operator delete(void *, void *) {
        // Deliberately empty placement delete operator.
        // Silences MSVC warning C4291: no matching operator delete found
    }
};


template<typename T>

// 错误发生在下一行

void createInto(void *memory) { new (memory) QQmlElement<T>; }

template<typename T>
QObject *createParent(QObject *p) { return new T(p); }

您正在明确定义您的构造函数。 如果不向构造函数传递任何参数怎么办? 应该使用哪个构造函数? QAbstractItemModel提供单个QObject*参数的参数,还是您使用两个 arguments 定义的参数? 您不能在没有任何 arguments 的情况下实例化JB_NodeModel ,因为您的编译器无法确定哪个构造函数是默认构造函数。 换句话说,调用new JB_NodeModel()将是模棱两可的,因此您的 class 将没有默认构造函数。

尝试将aJobID非默认值,看看它是否有效。 如果您必须默认 jobID=3,您也可以像这样创建构造函数:

explicit JB_NodeModel:JB_NodeModel(QObject *parent = nullptr):
QAbstractItemModel(parent),
jobID(3)
... 
{
 ...
}

关于它的一个非常奇怪的事情是你是如何成功编译这段代码的? 我假设您正在修剪代码以将其包含在此处,但为了让未来的读者清楚:

QAbstractItemModel是一个抽象的 class ,您至少必须实现它的纯虚函数。 根据关于子类化 QAbstractItemModel的 Qt 文档:

在继承 QAbstractItemModel 时,至少必须实现 index()、parent()、rowCount()、columnCount() 和 data()。 这些功能用于所有只读模型,并构成可编辑模型的基础。

解决了这个问题。 原来默认构造函数很好。 只是为了尝试一下,我尝试在堆栈上创建 JB_Node 并得到一个错误,说构造函数中有 int jobid(我已经取出)。 原来 my.pro 文件已损坏,并且该项目正在使用来自完全不同位置的 JB_NodeModel 文件(而不是在 Qt Creator 中打开的文件) - 不知道这是怎么发生的,但是一旦我修复了项目,一切都是很好,并且在 QML 引擎中注册的 class 正常。 现在我无法让 model 数据显示在 TreeView 中,所以如果我无法解决,我会发布它。 感谢那些回答的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM