Qt 6.11 is out! See what's new in the release
blog
C++ ListModel in QML - Its not connecting
-
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_HFunction 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 } } } -
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_HFunction 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 } } }@Sajjad-Ali try "as Peter"
I suspect the lowercase p is the issue.
-
S Sajjad Ali has marked this topic as solved on
-
@Sajjad-Ali try "as Peter"
I suspect the lowercase p is the issue.
Mr @GrecKo thanks its working. 🙂