I have problems when I pass a TreeViewModel to qml:
D: \ build-ViewshedGeoElement-Desktop_Qt_5_12_2_MSVC2017_64bit-Debug \ debug \ moc_ViewshedGeoElement.cpp: 153: Error: C2039: treemodellist: not a member of "ViewshedGeoElement"
Why is that ? What am I doing wrong? My code: main.cpp
#include <QGuiApplication> #include <QQuickView> #include <QCommandLineParser> #include <QDir> #include <QQmlEngine> #include <QSurfaceFormat> #ifdef Q_OS_WIN #include <Windows.h> #endif #include "ViewshedGeoElement.h" #include "coordinates.h" #include <QObjectList> #define STRINGIZE(x) #x #define QUOTE(x) STRINGIZE(x) #include <QDirIterator> #include "treeviewmodel.h" #include <QQmlContext> int main(int argc, char *argv[]) { QDirIterator it(":/Samples/Analysis/ViewshedGeoElement", QDirIterator::Subdirectories); QString exePath = QFileInfo(argv[0]).absolutePath(); while (it.hasNext()) { QString srcFileName = it.next(); QString dstFileName = exePath + "/../" + QFileInfo(srcFileName).fileName(); qDebug()<<QDir::currentPath(); if(!QFile::exists(dstFileName)) QFile::copy(srcFileName, dstFileName); } qRegisterMetaType<QObjectList>("QObjectList"); qmlRegisterType<TreeViewModel>("ru.host", 1, 0 , "TreeViewModel"); qmlRegisterType<QStandardItemModel>("ru.dom", 1, 0, "QStandardItemModel"); // qRegisterMetaType<TreeViewModel>("TreeViewModel"); #if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) // Linux requires 3.2 OpenGL Context // in order to instance 3D symbols QSurfaceFormat fmt = QSurfaceFormat::defaultFormat(); fmt.setVersion(3, 2); QSurfaceFormat::setDefaultFormat(fmt); #endif QGuiApplication app(argc, argv); app.setApplicationName("Viewshed Geoelement- C++"); // Initialize the sample ViewshedGeoElement::init(); // Initialize application view QQuickView view; view.setResizeMode(QQuickView::SizeRootObjectToView); QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH); QString arcGISToolkitImportPath = QUOTE(ARCGIS_TOOLKIT_IMPORT_PATH); #if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive); arcGISToolkitImportPath = arcGISToolkitImportPath.replace(replaceString, "linux", Qt::CaseSensitive); #endif // Add the import Path view.engine()->addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path view.engine()->addImportPath(arcGISRuntimeImportPath); // Add the Toolkit path view.engine()->addImportPath(arcGISToolkitImportPath); TreeViewModel treemodellist; view.engine()->rootContext()->setContextProperty("treemodellist",&treemodellist); // Set the source view.setSource(QUrl("qrc:/Samples/Analysis/ViewshedGeoElement/ViewshedGeoElement.qml")); view.show(); return app.exec(); } ViewshedGeoElement.qml
ViewshedGeoElementSample { ... Rectangle { width: parent.width Layout.fillHeight: true TreeView { width: parent.width height: parent.height model: treemodellist TableViewColumn { role: "display" title: "Списко 3D моделей" width: 1 } itemDelegate: Item { id: listTreeDelegateItem CheckBox { id: checkbox checked:false visible: styleData.value === undefined ? false : true MouseArea { onClicked: { if (parent.checked === true) { parent.checked = false } else { parent.checked = true } } } } Text { anchors.verticalCenter: parent.verticalCenter text: styleData.value === undefined ? "" : styleData.value leftPadding : 20 } } } } ... } treeviewmodel.h
#ifndef TREEVIEWMODEL_H #define TREEVIEWMODEL_H #include <QStandardItem> class TreeViewModel: public QStandardItemModel { Q_OBJECT public: TreeViewModel(QObject *parent=nullptr); }; Q_DECLARE_METATYPE(TreeViewModel*) #endif // TREEVIEWMODEL_H treeviewmodel.cpp
#include "treeviewmodel.h" #include <QStandardItem> #include "login.h" #include <QSqlQuery> #include <QSqlError> #include <QMessageBox> TreeViewModel::TreeViewModel(QObject *parent) : QStandardItemModel{parent} { setColumnCount(1); QStandardItem *rootItem = invisibleRootItem(); Login conn; conn.connOpen(); QSqlQuery qry(conn.mydb); if (qry.exec("SELECT id,name,url FROM \"models\"")) { while(qry.next()) { QStandardItem *value = new QStandardItem; value->setText(qry.value(1).toString()); rootItem->appendRow(value); qDebug()<<qry.value(0).toString(); qDebug()<<qry.value(1).toString(); qDebug()<<qry.value(2).toString(); } } else { qDebug()<<qry.lastError().text(); } } ViewshedGeoElement.h
class ViewshedGeoElement: public QQuickItem {Q_OBJECT ... Q_PROPERTY (TreeViewModel treemodellist READ treemodellist NOTIFY treemodellistChanged) ... private: TreeViewModel treemodellist; ...}
ViewshedGeoElement.cpp
... void ViewshedGeoElement :: componentComplete () {... emit treemodellistChanged ();
} ...
qmlRegisterType<TreeViewModel>("ru.host", 1, 0 , "TreeViewModel");- Alexander Chernin