Solved Error while using qmlRegisterType
-
Hi all,
Sorry if I'm creating a duplicated topic but I haven't found any solution that works for me.I'm newbie in Qt and currently I'm creating a C++ class named UserModel for using in qml file. This class calls a Restfull services then receives the result.
Here's the Header:
#ifndef USERMODEL_H #define USERMODEL_H #include <QObject> #include <QString> #include <QtNetwork> #include <QtNetwork/QNetworkRequest> #include <QtNetwork/QNetworkReply> class UserModel : public QObject { Q_OBJECT public: UserModel(); explicit UserModel(QObject *parent = Q_NULLPTR); QByteArray response; QNetworkReply::NetworkError error_type; QString error_str; //This calls the restfull service Q_INVOKABLE int checkLogin(QString username, QString password); signals: void executionFinished(UserModel *userModel); public slots: void replyFinished(QNetworkReply *reply); }; #endif // USERMODEL_H
In main.cpp file, after including header I used following line:
qmlRegisterType<UserModel>("com.quasimodo.user", 1, 0, "QUserModel");
However I have got these errors:
C:\Qt\Qt5.7.0\5.7\mingw53_32\include\QtQml\qqmlprivate.h:108: error: use of deleted function 'QQmlPrivate::QQmlElement<UserModel>::QQmlElement()'
void createInto(void *memory) { new (memory) QQmlElement<T>; }
^C:\Qt\Qt5.7.0\5.7\mingw53_32\include\QtQml\qqmlprivate.h:99: error: call of overloaded 'UserModel()' is ambiguous
Detailed built error:
Could anyone please show me what is the problem here?
Any help is much appreciated!
Thanks -
Hi
qqmlprivate.h:108: error: use of deleted function
This sounds like you try to create a copy of QObject
You are not allowed to clone or copy UserModel as its a QObject.
Maybe you try to put in a list ? Then list should be declared as UserModel *Sorry picture is very small. Cannot read it.
-
Your constructors are wrong (and I assume this is the reason
QQmlPrivate::createInto
fails). Consider your declaration:UserModel(); explicit UserModel(QObject *parent = Q_NULLPTR);
and now suppose I create an instance of that class, e.g.:
UserModel myModel;
How is the compiler supposed to know which of these two functions to call? They both match default construction. Fix this (by removing the implicit constructor for example) and see if the situation improves.
-
Thank you @mrjj very much for your support.
I updated the picture. Sorry but could you please explain a little bit more?
Here is my main.cpp file:
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include <usermodel.h> int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterType<UserModel>("com.quasimodo.user", 1, 0, "QUserModel"); QQmlApplicationEngine engine; engine.load(QUrl(QLatin1String("qrc:/main.qml"))); return app.exec(); }
Here is my qml file:
import QtQuick 2.7 import com.quasimodo.user 1.0 Page1Form { QUserModel{ id: user } button_Login.onClicked: { if (user.checkLogin(textInput_UserName.text.toString(), textInput_Password.text.toString()) === 1) { //.etc..
and that's all for my project.
Thanks.
-
@Khang
Well @kshegunov nailed it :)
(same errors as when try to copy/clone object so though it was that. sorry) -
@Khang If you have these two constructors:
UserModel(); explicit UserModel(QObject *parent = Q_NULLPTR);
and then write
UserModel myModel;
then the compiler cannot decide which of the two constructors to call. How would you decide?
Why do you have two constructors?
This one should be enough: explicit UserModel(QObject *parent = Q_NULLPTR); -
Thank you all for your help,
Yes that's right. The problem is because there are 2 constructors. After I removed one then the program works fine.Thanks.