I want to be able to access member variables of a C++ Model via QML
I have a ListModel:
ListModel {
id: credentialsModel
ListElement {
UserName: myCredentials.getUser
Password: myCredentials.getPassword
}
}
ListModel {
id: credentialsModel
ListElement {
UserName: myCredentials.getUser
Password: myCredentials.getPassword
}
}
To copy to clipboard, switch view to plain text mode
Which I am trying to fill with members of a class MyCredentials:
#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
#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
To copy to clipboard, switch view to plain text mode
This ListModel is inside a main.qml:
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")
}
}
}
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")
}
}
}
To copy to clipboard, switch view to plain text mode
Inside this main.qml a SwipeView with an component CredentialsView is used:
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
}
}
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
}
}
To copy to clipboard, switch view to plain text mode
The component CredentialsView contains a TableView:
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
}
}
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
}
}
To copy to clipboard, switch view to plain text mode
main.cpp:
#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();
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "mycredentials.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
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,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
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
Bookmarks