Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. C++ ListModel in QML - Its not connecting
Qt 6.11 is out! See what's new in the release blog

C++ ListModel in QML - Its not connecting

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 431 Views 1 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.
  • S Offline
    S Offline
    Sajjad Ali
    wrote on last edited by
    #1

    Hello, I have a list of colors created in C++ Model. I connected this to QML. But I get lots of errros..

    Please help me identify problem here. Thanks :-)

    Errors:

    Error compiling qml file: D:/WritingModel/WritingModel/main.qml:2:29: error: Invalid import qualifier ID
    mingw32-make.exe[2]: *** [CMakeFiles\appWritingModel.dir\build.make:117: .rcc/qmlcache/appWritingModel_main_qml.cpp] Error 1
    mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:97: CMakeFiles/appWritingModel.dir/all] Error 2
    mingw32-make.exe: *** [Makefile:135: all] Error 2
    22:08:33: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited with code 2.
    Error while building/deploying project WritingModel (kit: Desktop Qt 6.6.1 MinGW 64-bit)
    When executing step "Build"
    

    HeaderFile:

    #ifndef LISTMODEL_H
    #define LISTMODEL_H
    
    #include <QObject>
    #include <QAbstractListModel>
    #include <QColor>
    
    class ListModel : public QAbstractListModel
    {
        Q_OBJECT
    public:
        explicit ListModel(QObject *parent = nullptr);
    private:
        QStringList list;
    signals:
    
        // QAbstractItemModel interface
    public:
        int rowCount(const QModelIndex &parent) const;
        QVariant data(const QModelIndex &index, int role) const;
        QHash<int, QByteArray> roleNames() const;
    };
    
    #endif // LISTMODEL_H
    
    

    Function Declarations:

    #include "ListModel.h"
    
    ListModel::ListModel(QObject *parent)
        : QAbstractListModel(parent)
    {
        list = QColor::colorNames();
    }
    
    int ListModel::rowCount(const QModelIndex &parent) const
    {
        return list.size();
    }
    
    QVariant ListModel::data(const QModelIndex &index, int role) const
    {
        const int row = index.row();
        const QString result = list [row];
        return result;
    }
    
    QHash<int, QByteArray> ListModel::roleNames() const
    {
        QHash<int, QByteArray> mapping;
        mapping[Qt::DisplayRole] = "display";
        return mapping;
    }
    
    

    Main

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include "ListModel.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QQmlApplicationEngine engine;
        ListModel list;
        qmlRegisterSingletonInstance ("Sajjad.Models",1,0,"MyColors",&list);
        const QUrl url(u"qrc:/WritingModel/main.qml"_qs);
        QObject::connect(
            &engine,
            &QQmlApplicationEngine::objectCreated,
            &app,
            [url](QObject *obj, const QUrl &objUrl) {
                if (!obj && url == objUrl)
                    QCoreApplication::exit(-1);
            },
            Qt::QueuedConnection);
    
        engine.load(url);
        return app.exec();
    }
    

    Main.qml

    import QtQuick
    import Sajjad.Models 1.0 as peter
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        ListView {
            width: 300
            height: 300
            anchors.centerIn: parent
            model: peter.MyColors
            delegate: Text {
                text: display
            }
        }
    } 
    
    GrecKoG 1 Reply Last reply
    0
    • S Sajjad Ali

      Hello, I have a list of colors created in C++ Model. I connected this to QML. But I get lots of errros..

      Please help me identify problem here. Thanks :-)

      Errors:

      Error compiling qml file: D:/WritingModel/WritingModel/main.qml:2:29: error: Invalid import qualifier ID
      mingw32-make.exe[2]: *** [CMakeFiles\appWritingModel.dir\build.make:117: .rcc/qmlcache/appWritingModel_main_qml.cpp] Error 1
      mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:97: CMakeFiles/appWritingModel.dir/all] Error 2
      mingw32-make.exe: *** [Makefile:135: all] Error 2
      22:08:33: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited with code 2.
      Error while building/deploying project WritingModel (kit: Desktop Qt 6.6.1 MinGW 64-bit)
      When executing step "Build"
      

      HeaderFile:

      #ifndef LISTMODEL_H
      #define LISTMODEL_H
      
      #include <QObject>
      #include <QAbstractListModel>
      #include <QColor>
      
      class ListModel : public QAbstractListModel
      {
          Q_OBJECT
      public:
          explicit ListModel(QObject *parent = nullptr);
      private:
          QStringList list;
      signals:
      
          // QAbstractItemModel interface
      public:
          int rowCount(const QModelIndex &parent) const;
          QVariant data(const QModelIndex &index, int role) const;
          QHash<int, QByteArray> roleNames() const;
      };
      
      #endif // LISTMODEL_H
      
      

      Function Declarations:

      #include "ListModel.h"
      
      ListModel::ListModel(QObject *parent)
          : QAbstractListModel(parent)
      {
          list = QColor::colorNames();
      }
      
      int ListModel::rowCount(const QModelIndex &parent) const
      {
          return list.size();
      }
      
      QVariant ListModel::data(const QModelIndex &index, int role) const
      {
          const int row = index.row();
          const QString result = list [row];
          return result;
      }
      
      QHash<int, QByteArray> ListModel::roleNames() const
      {
          QHash<int, QByteArray> mapping;
          mapping[Qt::DisplayRole] = "display";
          return mapping;
      }
      
      

      Main

      #include <QApplication>
      #include <QQmlApplicationEngine>
      #include "ListModel.h"
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          QQmlApplicationEngine engine;
          ListModel list;
          qmlRegisterSingletonInstance ("Sajjad.Models",1,0,"MyColors",&list);
          const QUrl url(u"qrc:/WritingModel/main.qml"_qs);
          QObject::connect(
              &engine,
              &QQmlApplicationEngine::objectCreated,
              &app,
              [url](QObject *obj, const QUrl &objUrl) {
                  if (!obj && url == objUrl)
                      QCoreApplication::exit(-1);
              },
              Qt::QueuedConnection);
      
          engine.load(url);
          return app.exec();
      }
      

      Main.qml

      import QtQuick
      import Sajjad.Models 1.0 as peter
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
          ListView {
              width: 300
              height: 300
              anchors.centerIn: parent
              model: peter.MyColors
              delegate: Text {
                  text: display
              }
          }
      } 
      
      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @Sajjad-Ali try "as Peter"

      I suspect the lowercase p is the issue.

      S 1 Reply Last reply
      3
      • S Sajjad Ali has marked this topic as solved on
      • GrecKoG GrecKo

        @Sajjad-Ali try "as Peter"

        I suspect the lowercase p is the issue.

        S Offline
        S Offline
        Sajjad Ali
        wrote on last edited by
        #3

        Mr @GrecKo thanks its working. 🙂

        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