c++ - qml 未知方法返回类型 : ArchiveFile*, 即使调用了 qmlRegisterUncreatableType

标签 c++ qml qt5

简要说明我正在尝试做什么

我正在尝试制作一个应用程序来打开一些结构上非常相似的相关文件。 Í 有一个基类,名为 GenericFile和一个名为 ArchiveFile 的子类.也存在 ArchiveFile 的子类,其中包含特定存档文件格式的实现逻辑,但这些与 QML 无关,因此不会暴露给 QML。

我还有一个名为 FileManager 的类,它将某些方法公开给 QML 以创建 GenericFile 的实例并将它们向下转换为类似 ArchiveFile 的类, 基于 GenericFile.getFileCategory .

问题

我已经注册了 GenericFileArchiveFile使用 qmlRegisterUncreatableType ,像这样:

int main(int argc, char** argv) {
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;
    qmlRegisterUncreatableType<GenericFile>("me.henkkalkwater", 1, 0, "GenericFile", "Get your copy of GenericFile using FileManager!");
    qmlRegisterUncreatableType<ArchiveFile>("me.henkkalkwater", 1, 0, "ArchiveFile", "Get your copy of ArchiveFile using FileManager!");
    qmlRegisterSingletonType<FileManager>("me.henkkalkwater", 1, 0, "FileManager", [](QQmlEngine* engine, QJSEngine* jsEngine) -> QObject* {
        Q_UNUSED(engine)
        Q_UNUSED(jsEngine)
        return new FileManager();
    });
    const QUrl url(QStringLiteral("qrc:/qml/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);
}

调用 GenericFile* FileManager.getFile(int index)在 QML 中工作正常。但是调用ArchiveFile* FileManager.getArchive(int index)不起作用,并显示消息“错误:未知方法返回类型:ArchiveFile*”。

问题与 ArchiveFile 有关继承自 GenericFile还是我在其他地方犯了一个愚蠢的错误?还是我的类(class)设计存在严重缺陷?我是否将一些 Java 约定应用于 C++ 而应该以另一种方式完成?

我的代码

通用文件.h:

class GenericFile;
typedef GenericFile* (*CreateFileFunction) (QIODevice* file, QString fileName, QObject* parent);

class GenericFile : public QObject {
    Q_OBJECT
public:
    enum FileCategory {
        UNKNOWN,
        COMPRESSED,
        ARCHIVE,
        LAYOUT
    };
    Q_ENUM(FileCategory)

    GenericFile(QIODevice* source, QString name, QObject* parent = nullptr);
    virtual ~NinFile();

    Q_PROPERTY(QString fileName READ getFileName)
    Q_PROPERTY(FileCategory fileCategory READ getFileCategory)

    /**
     * @brief Register a file usign a certain string of magic bytes.
     * @param magic
     * @param fn
     */
    static void RegisterFile(QLatin1String magic, CreateFileFunction fn);

    /**
     * @brief Return one of the subclasses of the file.
     * @param file The file to determine.
     * @return The class able to parse this file format.
     *
     * Scans the magic bytes and returns a subclass of GenericFile that represents the file.
     * The subclass will take ownership of the QFile and destroy it whenever it is destroyed
     * itself.
     */
    static GenericFile* fromFile(QFile* file, QObject* parent = nullptr);
    static GenericFile* fromIODevice(QIODevice* file, QString filename, QObject* parent = nullptr);

    /**
     * @brief Initialize the default file associations.
     */
    static void init();

    /**
     * @return The filename of this file
     */
    QString getFileName() const {
        return fileName;
    }

    /**
     * @return return the general category the file belongs in
     */
    virtual FileCategory getFileCategory() const {
        return UNKNOWN;
    }

protected:
    QIODevice* file;
    QString fileName;
};

归档文件.h

class ArchiveFile : public GenericFile
{
public:
    ArchiveFile(QIODevice* device, QString fileName, QObject* parent = nullptr);

    Q_INVOKABLE
    virtual QList<QString> listContents() = 0;
    FileCategory getFileCategory() const override { return GenericFile::ARCHIVE; }
};

FileManager.h(包含相关方法实现)

class FileManager : public QAbstractListModel
{
    Q_OBJECT
public:
    explicit FileManager(QObject *parent = nullptr);
    enum RoleNames {
        INDEX = Qt::UserRole + 1,
        PATH,
        TYPE,
        CATEGORY
    };

    /**
     * @brief Opens a file
     * @param filePath The path to the file
     * @return false if the opening fails, true otherwise
     * Opens a file, and if successful, returns true and adds it to this model. Otherwise, it returns false.
     */
    Q_INVOKABLE
    bool openFile(QUrl filePath);

    Q_INVOKABLE
    GenericFile* getFile(int index) {
        if (index >= 0 && index < files.length()) {
            return files[index];
        }
        return nullptr;
    }

    Q_INVOKABLE
    ArchiveFile* getArchive(int index) {
        qDebug() << "Callled getArchive";
        try {
            return dynamic_cast<ArchiveFile*>(getFile(index));
        } catch (std::bad_cast e){
            return nullptr;
        }
    }

    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
    int rowCount(const QModelIndex& parent = QModelIndex()) const override;
    QHash<int, QByteArray> roleNames() const override;

private:
    QList<GenericFile*> files;
};

编辑:似乎每次我注册一个基类和所述基类的子类时都会发生这种情况。

最佳答案

Q_OBJECT 宏添加到子类 (ArchiveFile) 似乎可以解决问题。我错误地认为您只需以某种方式在基类 (GenericFile) 中定义 Q_OBJECT 宏。

关于c++ - qml 未知方法返回类型 : ArchiveFile*, 即使调用了 qmlRegisterUncreatableType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57077967/

相关文章:

qt - 在qml中动态添加选项卡

c++ - 在 OpenCV 中移动图像内容

c++ - 为什么调用 move 分配?

c++ - QTabWidget 选项卡旁边的 QWidget

linux - 如何使用纯qml linux获取系统,用户信息

c++ - 如何在 qt 中更改一个 QBarSet 栏/元素颜色?

C++ 编译器错误 : "isLeaf() has not been declared" - but it was

qml - 子元素能否均匀填充Grid?

c++ - Qt Qml中的圆形图像

c++ - 当单击按钮隐藏位于其他类中的另一个按钮时