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:
Code:
ListModel {
id: credentialsModel
ListElement {
UserName: myCredentials.getUser
Password: myCredentials.getPassword
}
}
Which I am trying to fill with members of a class MyCredentials:
Code:
#ifndef MYCREDENTIALS_H
#define MYCREDENTIALS_H
#pragma once
#include <iostream>
#include <QObject>
class MyCredentials
: public QObject{
Q_OBJECT
Q_PROPERTY(std::string* getUser READ getUser WRITE setUser)
Q_PROPERTY(std::string* getPassword READ getPassword WRITE setPassword)
public:
explicit MyCredentials
(std
::string* user, std
::string* password,
QObject *parent
= nullptr
): m_user
(user
),m_password
(password
) {};
void print() const{
std::cout<<"user "<<*m_user<<std::endl;
std::cout<<"password "<<*m_password<<std::endl;
std::cout.flush();
}
std::string* getUser() const
{
return m_user;
}
void setUser(std::string* value)
{
m_user = value;
}
std::string* getPassword() const
{
return m_password;
}
void setPassword(std::string* value)
{
m_password = value;
}
private:
std::string* m_user;
std::string* m_password;
};
#endif // MYCREDENTIALS_H
This ListModel is inside a main.qml:
Code:
import QtQuick 2.12
import QtQuick.Controls 2.5
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Tabs")
ListModel {
id: credentialsModel
ListElement {
// Thats my goal: I want to get member variables of the ContextProperty myList
// Why this error?: ListElement: cannot use script for property value
UserName: myCredentials.getUser
Password: myCredentials.getPassword
}
}
SwipeView {
id: swipeView
anchors.fill: parent
currentIndex: tabBar.currentIndex
CredentialsView{
id: dataBasesView
}
}
footer: TabBar {
id: tabBar
currentIndex: swipeView.currentIndex
TabButton {
text: qsTr("Credentials")
}
}
}
Inside this main.qml a SwipeView with an component CredentialsView is used:
Code:
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.3
Page {
id: page
width: 600
height: 400
header: Label {
text: qsTr("Credentials")
font.pixelSize: Qt.application.font.pixelSize * 2
padding: 10
}
CredentialsTableView{
anchors.fill: parent
model: credentialsModel
}
}
The component CredentialsView contains a TableView:
Code:
import QtQuick 2.9
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
TableView {
id: credentialsTableView
TableViewColumn {
role: "UserName"
title: "User Name"
width: credentialsTableView.width / 2
}
TableViewColumn {
role: "Password"
title: "Password"
width: credentialsTableView.width / 2
}
}
main.cpp:
Code:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "mycredentials.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
std::string* myUser;
std::string user("usr");
myUser = &user;
std::string* myPassword;
std::string password("psw");
myPassword = &password;
MyCredentials myCredentials(myUser, myPassword);
myCredentials.print();
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty(QStringLiteral("myCredentials"), &myCredentials);
const QUrl url
(QStringLiteral
("qrc:/main.qml"));
QObject::connect(&engine,
&QQmlApplicationEngine
::objectCreated,
if (!obj && url == objUrl)
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
Code is available via https://github.com/hstig/qt.
Where is my error? What should I do?
Many thanks
Re: Accessing C++ Model via QML throws error: ListElement: cannot use script for prop
Re: Accessing C++ Model via QML throws error: ListElement: cannot use script for prop
Quote:
Originally Posted by
peppermg
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? :confused:
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