base class QSortFilterProxyModel has private copy constructor
-
Need help, I'm a Seasoned developer and I've been working with QML for about a year but I'm very new to C++ and I'm working on a project to convert a pyqt application to a pure C++ application. We have an app that is primarily all QML with several different sort/filter lists. In the existing pyqt app we can generate a QSortFilterProxyModel from QML and set it as the source model to any List within QML but I have so far failed to convert this functionality to a pure C++ solution. Here are the relevant code Snippets that I have so far:
::main.cpp
int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; RootContext rrc; QQmlContext *rootContext = engine.rootContext(); /*this lets me use a single rootContext QObject from C++ to have access to global properties and slots within the QML application*/ rootContext->setContextObject(&rrc); engine.load(QUrl(QStringLiteral("(path to root .qml file)"))); QObject *topLevel = engine.rootObjects().value(0); QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); window->show(); return app.exec(); }
:: rootContext.h
#include <filtermodel.h> ... class RootContext : public QObject { Q_OBJECT public: explicit RootContext(QObject* parent = 0); public slots: // If I remove this slot from the header and the C++ then everything compiles ok FilterModel GetSortFilterModel(QObject *parent); }; ...
:: rootContext.cpp
#include <filtermodel.h> ... RootContext::RootContext(QObject* parent) : QObject(parent) { } // This method causes the compile error: base class QSortFilterProxyModel has private copy constructor FilterModel RootContext::GetSortFilterModel(QObject *parent) { return new FilterModel(parent); } ...
:: filtermodel.h
#ifndef SORTFILTERMODEL_H #define SORTFILTERMODEL_H #include <QSortFilterProxyModel> class FilterModel: public QSortFilterProxyModel { Q_OBJECT public: FilterModel(QObject * parent = 0); ~FilterModel(); signals: public slots: }; #endif // SORTFILTERMODEL_H
:: filtermodel.cpp
#include "filtermodel.h" FilterModel::FilterModel(QObject* parent): QSortFilterProxyModel(parent) { } FilterModel::~FilterModel() { }
Then we can generate a new sortfilter model from QML which we currently do in the pyqt app all over the place like this:
Component { Item { width: parent.width height: childrenRect.height property var filteredModel: GetSortFilterModel(gridView) Binding { target: filteredModel property: 'source' value: (Pre-generated ListModel of items) } GridView { id: gridView model: filteredModel width: parent.width delegate: someDelegate } } }
compile output:
In file included from ..rootcontext.cpp:1:
In file included from ..rootcontext.h:7:
../filtermodel.h:7:7: error: base class 'QSortFilterProxyModel' has private copy constructor
class FilterModel: public QSortFilterProxyModel
^
../../../../Qt5.5/5.5/clang_64/lib/QtCore.framework/Headers/qsortfilterproxymodel.h:162:20: note: declared private here
Q_DISABLE_COPY(QSortFilterProxyModel)
^
.../Qt5.5/5.5/clang_64/lib/QtCore.framework/Headers/qglobal.h:1042:5: note: expanded from macro 'Q_DISABLE_COPY'
Class(const Class &) Q_DECL_EQ_DELETE;
^
..rootcontext.cpp:57:5: note: implicit copy constructor for 'FilterModel' first required here
return new FilterModel(parent);
^
1 error generated.
make: *** [rootcontext.o] Error 1
14:27:04: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Pumpernickel (kit: Desktop QT5.5)
When executing step "Make" -
Hi,
FilterModel RootContext::GetSortFilterModel(QObject *parent)
should rather beFilterModel* RootContext::GetSortFilterModel(QObject *parent)
Otherwise you are trying to copy a QObject derived class which is not possible.
-
Thank you thank you, that totally works, It solved my compile error, I then started getting a runtime error in QML saying "unknown method return type FilterModel*" but once I regestered that type with QML its now working. Quick question, I'm currently registering that type in my main.cpp file like so:
qmlRegisterType<FilterModel>("FilterModel", 1, 0, "FilterModel");
I was just wondering if there was a way to include the type in the QML_IMPORT_PATH property or something, or if I'll just have to use the former example to register all my custom QObject types?
-
You have to register your custom types