Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error while using qmlRegisterType

Error while using qmlRegisterType

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 3.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Khang
    wrote on last edited by Khang
    #1

    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:
    alt text

    Could anyone please show me what is the problem here?
    Any help is much appreciated!
    Thanks

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • K Offline
          K Offline
          Khang
          wrote on last edited by
          #4

          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.

          mrjjM jsulmJ 2 Replies Last reply
          0
          • K Khang

            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.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Khang
            Well @kshegunov nailed it :)
            (same errors as when try to copy/clone object so though it was that. sorry)

            1 Reply Last reply
            0
            • K Khang

              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.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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);

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • K Offline
                K Offline
                Khang
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved