I want to be able to access member variables of a C++ Model via QML

I have a ListModel:

Qt Code:
  1. ListModel {
  2. id: credentialsModel
  3. ListElement {
  4. UserName: myCredentials.getUser
  5. Password: myCredentials.getPassword
  6. }
  7. }
To copy to clipboard, switch view to plain text mode 

Which I am trying to fill with members of a class MyCredentials:

Qt Code:
  1. #ifndef MYCREDENTIALS_H
  2. #define MYCREDENTIALS_H
  3.  
  4. #pragma once
  5.  
  6. #include <iostream>
  7. #include <QObject>
  8.  
  9. class MyCredentials: public QObject
  10. {
  11. Q_OBJECT
  12. Q_PROPERTY(std::string* getUser READ getUser WRITE setUser)
  13. Q_PROPERTY(std::string* getPassword READ getPassword WRITE setPassword)
  14.  
  15. public:
  16. explicit MyCredentials(std::string* user, std::string* password, QObject *parent = nullptr): m_user(user),m_password(password) {};
  17.  
  18. void print() const{
  19. std::cout<<"user "<<*m_user<<std::endl;
  20. std::cout<<"password "<<*m_password<<std::endl;
  21. std::cout.flush();
  22. }
  23.  
  24. std::string* getUser() const
  25. {
  26. return m_user;
  27. }
  28.  
  29. void setUser(std::string* value)
  30. {
  31. m_user = value;
  32. }
  33.  
  34. std::string* getPassword() const
  35. {
  36. return m_password;
  37. }
  38.  
  39. void setPassword(std::string* value)
  40. {
  41. m_password = value;
  42. }
  43.  
  44. private:
  45. std::string* m_user;
  46. std::string* m_password;
  47. };
  48.  
  49. #endif // MYCREDENTIALS_H
To copy to clipboard, switch view to plain text mode 


This ListModel is inside a main.qml:

Qt Code:
  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.5
  3.  
  4. ApplicationWindow {
  5. visible: true
  6. width: 640
  7. height: 480
  8. title: qsTr("Tabs")
  9.  
  10. ListModel {
  11. id: credentialsModel
  12. ListElement {
  13.  
  14. // Thats my goal: I want to get member variables of the ContextProperty myList
  15. // Why this error?: ListElement: cannot use script for property value
  16. UserName: myCredentials.getUser
  17. Password: myCredentials.getPassword
  18. }
  19. }
  20.  
  21. SwipeView {
  22. id: swipeView
  23. anchors.fill: parent
  24. currentIndex: tabBar.currentIndex
  25.  
  26. CredentialsView{
  27. id: dataBasesView
  28.  
  29. }
  30. }
  31.  
  32. footer: TabBar {
  33. id: tabBar
  34. currentIndex: swipeView.currentIndex
  35.  
  36. TabButton {
  37. text: qsTr("Credentials")
  38. }
  39. }
  40. }
To copy to clipboard, switch view to plain text mode 

Inside this main.qml a SwipeView with an component CredentialsView is used:

Qt Code:
  1. import QtQuick 2.9
  2. import QtQuick.Controls 2.2
  3. import QtQuick.Layouts 1.3
  4.  
  5. Page {
  6. id: page
  7. width: 600
  8. height: 400
  9.  
  10. header: Label {
  11. text: qsTr("Credentials")
  12. font.pixelSize: Qt.application.font.pixelSize * 2
  13. padding: 10
  14. }
  15.  
  16. CredentialsTableView{
  17. anchors.fill: parent
  18. model: credentialsModel
  19. }
  20.  
  21. }
To copy to clipboard, switch view to plain text mode 

The component CredentialsView contains a TableView:

Qt Code:
  1. import QtQuick 2.9
  2. import QtQuick.Controls 1.4
  3. import QtQuick.Layouts 1.3
  4.  
  5. TableView {
  6. id: credentialsTableView
  7. TableViewColumn {
  8. role: "UserName"
  9. title: "User Name"
  10. width: credentialsTableView.width / 2
  11. }
  12. TableViewColumn {
  13. role: "Password"
  14. title: "Password"
  15. width: credentialsTableView.width / 2
  16. }
  17. }
To copy to clipboard, switch view to plain text mode 


main.cpp:
Qt Code:
  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. #include <QQmlContext>
  4.  
  5. #include "mycredentials.h"
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  10.  
  11. QGuiApplication app(argc, argv);
  12.  
  13. std::string* myUser;
  14. std::string user("usr");
  15. myUser = &user;
  16.  
  17. std::string* myPassword;
  18. std::string password("psw");
  19. myPassword = &password;
  20.  
  21. MyCredentials myCredentials(myUser, myPassword);
  22.  
  23. myCredentials.print();
  24.  
  25. QQmlApplicationEngine engine;
  26. engine.rootContext()->setContextProperty(QStringLiteral("myCredentials"), &myCredentials);
  27.  
  28. const QUrl url(QStringLiteral("qrc:/main.qml"));
  29. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  30. &app, [url](QObject *obj, const QUrl &objUrl) {
  31. if (!obj && url == objUrl)
  32. QCoreApplication::exit(-1);
  33. }, Qt::QueuedConnection);
  34. engine.load(url);
  35.  
  36. return app.exec();
  37. }
To copy to clipboard, switch view to plain text mode 

Code is available via https://github.com/hstig/qt.

Where is my error? What should I do?

Many thanks