How to extend QFileSystemModel (inheritance)
-
Hello,
I am trying to create a class that inherits from QFileSystemModel.
I tried to create a Shared Library to expose the QFileSystemModel overrided methods, but several linking errors were shown.
I want to do that without recompiling the whole Qt source-code. The main goal is to dynamically link the extended class. Is that possible?Here is the error message:
...
qremotestoragemodel3.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QFileSystemModel::QFileSystemModel(class QObject *)" (_imp??0QFileSystemModel@@QAE@PAVQObject@@@Z) referenced in function "public: __thiscall QRemoteStorageModel3::QRemoteStorageModel3(class QObject *)" (??0QRemoteStorageModel3@@QAE@PAVQObject@@@Z)
...Any suggestion?
Thanks.
-
Hi and welcome to devnet,
Can you show your code ?
-
Seems your shared library is not linked to the Qt libraries. Are you linking it to the widgets module?
-
@SGaist I appreciate the community is very active here, thanks.
Here is my code:
// main.cpp
#include <QCoreApplication>
#include "qremotestoragemodel3.h"int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QRemoteStorageModel3 *qmi = new QRemoteStorageModel3();
//QModelIndex qModelIndex;
//qmi->remove(qModelIndex);
printf(qmi->getString());
return a.exec();
}//qremotestoragemodel3.cpp
#include "qremotestoragemodel3.h"QT_BEGIN_NAMESPACE
#ifndef QT_NO_FILESYSTEMMODEL
//constructor
QRemoteStorageModel3::QRemoteStorageModel3(QObject *parent) : QFileSystemModel(parent){
}//Just to test the compilation
char *QRemoteStorageModel3 :: getString(){
return "a";
}//Trying to re-implement the remove method from QFileSystemModel
bool QRemoteStorageModel3 :: remove(const QModelIndex &index)
{
printf("Hello world!! \n");//Old remove() code from QFileSystemModel /*
const QString path = filePath(aindex);
QFileSystemModelPrivate * d = const_cast<QFileSystemModelPrivate*>(d_func());
#ifndef QT_NO_FILESYSTEMWATCHER
d->fileInfoGatherer.removePath(path);
#endif
if (QFileInfo(path).isFile())
return QFile::remove(path);
return QDir(path).removeRecursively();
*/return true;
}
#endif
QT_END_NAMESPACE//qremotestoragemodel3.h
#ifndef QREMOTESTORAGEMODEL3_H
#define QREMOTESTORAGEMODEL3_H#include <QtCore/qglobal.h>
#include <QtWidgets/qfilesystemmodel.h>QT_BEGIN_NAMESPACE
class QRemoteStorageModel3: public QFileSystemModel{
public:
explicit QRemoteStorageModel3(QObject *parent = 0);
char *getString();
bool remove(const QModelIndex &index);
};QT_END_NAMESPACE
#endif // QREMOTESTORAGEMODEL3_H
-
@Chris-Kawa Hi Chris, I just posted my code here.
I am not sure if I have to do something in my code in order to link to the Qt libraries, or is it something in the project properties?
Thank you for your attention. -
From your code, it looks like you are mixing subclassing for your project and subclassing to add a new class in Qt's source.
If you only want to create a new subclass do not copy code from Qt's implementation like that. Just write your code and call the base class implementation when needed.
-
It depends on your build system. By the look of the message you're using compiler from Visual Studio.
If you're building from within Qt Creator using qmake then you need to make sure the .pro file contains this:
QT += gui widgets
If you're building from within Visual Studio using the VS Add-in then right click on the project in the solution explorer, select Qt Project Settings and on the Qt Modules tab make sure GUI and Widgets are selected.
-
@SGaist Thank you. I am studying more about how to create a new subclass. In fact, I don't want to call the base class implementation, I just want to re-implement it, because the FileSystemModel class deals with the local file system, and I want my subclass to inherit from FilesystemModel but my implementation will deal with remote file systems.
Thank you again. -
@Chris-Kawa I am building from within Qt Creator (Windows 7), my .pro file didn't contain that! it just had:
QT += gui
Thank you Chris.