Results 1 to 4 of 4

Thread: Accessing C++ Model via QML throws error: ListElement: cannot use script for property

  1. #1
    Join Date
    Apr 2021
    Posts
    6
    Qt products
    Qt5

    Default Accessing C++ Model via QML throws error: ListElement: cannot use script for property

    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

  2. #2
    Join Date
    Dec 2008
    Posts
    18
    Thanks
    4
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing C++ Model via QML throws error: ListElement: cannot use script for prop

    QML doesn't know what std::string* is. https://doc.qt.io/qt-5/qtqml-cppinte...-qt-data-types

  3. #3
    Join Date
    Apr 2021
    Posts
    6
    Qt products
    Qt5

    Default Re: Accessing C++ Model via QML throws error: ListElement: cannot use script for prop

    Quote Originally Posted by peppermg View Post
    QML doesn't know what std::string* is. https://doc.qt.io/qt-5/qtqml-cppinte...-qt-data-types
    Thanks, but same error with QString*. I think there is no way to use the static ListElement in QML. Maybe it is not allowed to change the values inside ListModel: https://www.ics.com/webinar/vbhdsgjashgd at Minute 3:35. Or is it possible somehow anyway?

  4. #4
    Join Date
    Dec 2008
    Posts
    18
    Thanks
    4
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Accessing C++ Model via QML throws error: ListElement: cannot use script for prop

    It also won't know what a QString * is, don't use pointers here. I am not sure what your end goal is with this, but look at sub-classing QAbstractListModel. https://doc.qt.io/qt-5/qtquick-model...model-subclass

Similar Threads

  1. Accessing model's QQmlListProperty property in QML
    By ElDiabloComputatore in forum Qt Quick
    Replies: 3
    Last Post: 12th December 2014, 08:20
  2. Qt 5 QSystemTrayIcon throws error message on Mac OS X
    By janusz in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2013, 15:55
  3. Replies: 1
    Last Post: 8th February 2012, 03:13
  4. Replies: 0
    Last Post: 22nd April 2011, 11:20
  5. qt script property
    By wookoon in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2010, 11:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.