![](/img/trans.png)
[英]QtQuick - qml:28: Error: Unknown method return type: custom type
[英]qml Unknown method return type: ArchiveFile*, even though qmlRegisterUncreatableType was called
我正在嘗試創建一個應用程序來打開一些結構相似的相關文件。 Í有一個名為GenericFile
的基類和一個名為ArchiveFile
的子類。 還存在ArchiveFile
子類,它包含特定存檔文件格式的實現邏輯,但這些子類與QML無關,因此不會暴露給QML。
我還有另一個名為FileManager
類,它將某些方法暴露給QML以創建GenericFile
實例,並將它們ArchiveFile
到ArchiveFile
等基於GenericFile.getFileCategory
。
我已經注冊都GenericFile
和ArchiveFile
使用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)
不起作用,消息“Error:Unknown method return type:ArchiveFile *”。
已經做的事實問題ArchiveFile
從繼承GenericFile
還是我做出愚蠢的錯誤別的地方? 或者我的課程設計嚴重缺陷? 我是否將一些Java約定用於C ++,這應該以另一種方式完成?
genericfile.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;
};
archivefile.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
宏。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.