How can i use Model c++ class in cascade with 2 other repeater in .qml file
I have followed this tutorial : https://resources.qt.io/resources-by-content-type-videos-demos-tutorials/using-c-models-in-qml-tutorial
This was usefull to create my c++ class to manage my LightModel
Now I want to re-use this tutorial where I have a model inside 2 other repeaters in cascade. The issue is that my qml code instantiate LightModel objects inside repeaters so I don't have access to these instances from main.cpp
I used qmlRegisterType to give qml the ability to create several objects from my LightModel class.
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<Light>("Light",1,0,"Light");
qmlRegisterType<LightModel>("Light", 1,0,"LightModel");
qmlRegisterUncreatableType<Light>("Light", 1, 0, "LightList", QStringLiteral("LightList should not be created in QML"));
QQmlApplicationEngine engine;
MainWindow w;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
#include <QVector>
struct LightItem{
QString description;
int value;
int bornInf;
int bornSup;
int decimals; //1, 10
bool enabled;
};
class Light : public QObject
{
Q_OBJECT
public:
explicit Light(QObject *parent = nullptr);
QVector<LightItem> items() const;
bool setItemAt(int index, const LightItem &item);
signals:
void preItemAppended();
void postItemAppended();
void preItemRemoved(int index);
void postItemRemoved();
private:
QVector<LightItem> mItems;
};
#endif // LIGHT_H
lightmodel.h
#ifndef LIGHTMODEL_H
#define LIGHTMODEL_H
#include <QAbstractListModel>
class Light;
class LightModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(Light *list READ list WRITE setList)
Q_PROPERTY(int ledMode READ getLedMode WRITE setLedMode)
public:
explicit LightModel(QObject *parent = nullptr);
enum{
DescriptionRole = Qt::UserRole,
ValueRole,
BornInfRole,
BornSupRole,
Decimals,
EnableRole
};
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual QHash<int, QByteArray> roleNames() const override;
Light *list() const;
void setList(Light *list);
int getLedMode() const;
void setLedMode(int value);
private:
Light *mList;
int ledMode;
};
#endif // LIGHTMODEL_H
main.qml
StackLayout {
id: lampChose
currentIndex: gridLamps.indexImage
Repeater {
id: repeater1
model: 24
StackLayout {
id: lampStack
currentIndex: tabBarNm.currentIndex
Repeater {
id: repeater2
model: 7
Column {
id: columnSpinBoxtConfLeds
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: LightModel {
id: lightModel
list: light }
SpinBox {
id: spinBoxTest
visible: true
editable: true
value: model.value
from: model.bornInf
to: model.bornSup
stepSize: 1
onValueChanged: {
model.value = value
}
}
}
}
}
}
}
My LightModel class is implemented like ToDoModel in the tutorial exemple : https://github.com/mitchcurtis/todo-list-tutorial/tree/chapter-11
So the question is : how can I access from C++ the content of the LightModel objects because the 24x7 instances are created by .qml file.
Thanks a lot for your time
c++ qt qml
|
show 7 more comments
I have followed this tutorial : https://resources.qt.io/resources-by-content-type-videos-demos-tutorials/using-c-models-in-qml-tutorial
This was usefull to create my c++ class to manage my LightModel
Now I want to re-use this tutorial where I have a model inside 2 other repeaters in cascade. The issue is that my qml code instantiate LightModel objects inside repeaters so I don't have access to these instances from main.cpp
I used qmlRegisterType to give qml the ability to create several objects from my LightModel class.
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<Light>("Light",1,0,"Light");
qmlRegisterType<LightModel>("Light", 1,0,"LightModel");
qmlRegisterUncreatableType<Light>("Light", 1, 0, "LightList", QStringLiteral("LightList should not be created in QML"));
QQmlApplicationEngine engine;
MainWindow w;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
#include <QVector>
struct LightItem{
QString description;
int value;
int bornInf;
int bornSup;
int decimals; //1, 10
bool enabled;
};
class Light : public QObject
{
Q_OBJECT
public:
explicit Light(QObject *parent = nullptr);
QVector<LightItem> items() const;
bool setItemAt(int index, const LightItem &item);
signals:
void preItemAppended();
void postItemAppended();
void preItemRemoved(int index);
void postItemRemoved();
private:
QVector<LightItem> mItems;
};
#endif // LIGHT_H
lightmodel.h
#ifndef LIGHTMODEL_H
#define LIGHTMODEL_H
#include <QAbstractListModel>
class Light;
class LightModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(Light *list READ list WRITE setList)
Q_PROPERTY(int ledMode READ getLedMode WRITE setLedMode)
public:
explicit LightModel(QObject *parent = nullptr);
enum{
DescriptionRole = Qt::UserRole,
ValueRole,
BornInfRole,
BornSupRole,
Decimals,
EnableRole
};
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual QHash<int, QByteArray> roleNames() const override;
Light *list() const;
void setList(Light *list);
int getLedMode() const;
void setLedMode(int value);
private:
Light *mList;
int ledMode;
};
#endif // LIGHTMODEL_H
main.qml
StackLayout {
id: lampChose
currentIndex: gridLamps.indexImage
Repeater {
id: repeater1
model: 24
StackLayout {
id: lampStack
currentIndex: tabBarNm.currentIndex
Repeater {
id: repeater2
model: 7
Column {
id: columnSpinBoxtConfLeds
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: LightModel {
id: lightModel
list: light }
SpinBox {
id: spinBoxTest
visible: true
editable: true
value: model.value
from: model.bornInf
to: model.bornSup
stepSize: 1
onValueChanged: {
model.value = value
}
}
}
}
}
}
}
My LightModel class is implemented like ToDoModel in the tutorial exemple : https://github.com/mitchcurtis/todo-list-tutorial/tree/chapter-11
So the question is : how can I access from C++ the content of the LightModel objects because the 24x7 instances are created by .qml file.
Thanks a lot for your time
c++ qt qml
Hi there, welcome to Stack Overflow! I really appreciate the inclusion of code and background of your issue. What is slightly bugging me, is the question at the end.how can I access
. Can you clarify here? Access what? Access how? Individual objects ofLightModel
? I would love it if you could clarify this. Thanks!
– TrebuchetMS
Nov 14 '18 at 10:19
The easiest solution (but maybe not the best) would be to register eachLightModel
from the constructor to theLightList
. But it really depends on why you want to access the Lights. From theid
it seems you are trying to create some sort of shop-gallery?
– Amfasis
Nov 14 '18 at 10:35
Hi, thank you both for your answer, @TrebuchetMS I would like to have the reference on the created objects because I want to do backend stuff with it. For exemple, this qml file create 24x7 instance of LightModel class, the user modify theses instances with the HMI, but I don't manage to reach these instances from c++. I don't know if you understand what I mean ?
– C. Duval
Nov 14 '18 at 10:44
@Amfasis, this will take a lot of lines in c++ file and a huge amount of lines in .qml file to register instances one by one and keep trace of them that way I think...
– C. Duval
Nov 14 '18 at 10:44
1
Hi, I update the post to integrate my LightModel.h and Light.h The updates are working fine because i can log and see model.value changing, but I have no way to see it from main.cpp In other words : i would like to correlate the instances LightModel created 24x7 times and identify each one with both index of repeater1 and repeater2 (generated by "model :24 and model:7") At the end I would like to have for example an array or a vector like :LightModel mArray[24][7]
which reference all the instances by index them.
– C. Duval
Nov 14 '18 at 13:01
|
show 7 more comments
I have followed this tutorial : https://resources.qt.io/resources-by-content-type-videos-demos-tutorials/using-c-models-in-qml-tutorial
This was usefull to create my c++ class to manage my LightModel
Now I want to re-use this tutorial where I have a model inside 2 other repeaters in cascade. The issue is that my qml code instantiate LightModel objects inside repeaters so I don't have access to these instances from main.cpp
I used qmlRegisterType to give qml the ability to create several objects from my LightModel class.
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<Light>("Light",1,0,"Light");
qmlRegisterType<LightModel>("Light", 1,0,"LightModel");
qmlRegisterUncreatableType<Light>("Light", 1, 0, "LightList", QStringLiteral("LightList should not be created in QML"));
QQmlApplicationEngine engine;
MainWindow w;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
#include <QVector>
struct LightItem{
QString description;
int value;
int bornInf;
int bornSup;
int decimals; //1, 10
bool enabled;
};
class Light : public QObject
{
Q_OBJECT
public:
explicit Light(QObject *parent = nullptr);
QVector<LightItem> items() const;
bool setItemAt(int index, const LightItem &item);
signals:
void preItemAppended();
void postItemAppended();
void preItemRemoved(int index);
void postItemRemoved();
private:
QVector<LightItem> mItems;
};
#endif // LIGHT_H
lightmodel.h
#ifndef LIGHTMODEL_H
#define LIGHTMODEL_H
#include <QAbstractListModel>
class Light;
class LightModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(Light *list READ list WRITE setList)
Q_PROPERTY(int ledMode READ getLedMode WRITE setLedMode)
public:
explicit LightModel(QObject *parent = nullptr);
enum{
DescriptionRole = Qt::UserRole,
ValueRole,
BornInfRole,
BornSupRole,
Decimals,
EnableRole
};
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual QHash<int, QByteArray> roleNames() const override;
Light *list() const;
void setList(Light *list);
int getLedMode() const;
void setLedMode(int value);
private:
Light *mList;
int ledMode;
};
#endif // LIGHTMODEL_H
main.qml
StackLayout {
id: lampChose
currentIndex: gridLamps.indexImage
Repeater {
id: repeater1
model: 24
StackLayout {
id: lampStack
currentIndex: tabBarNm.currentIndex
Repeater {
id: repeater2
model: 7
Column {
id: columnSpinBoxtConfLeds
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: LightModel {
id: lightModel
list: light }
SpinBox {
id: spinBoxTest
visible: true
editable: true
value: model.value
from: model.bornInf
to: model.bornSup
stepSize: 1
onValueChanged: {
model.value = value
}
}
}
}
}
}
}
My LightModel class is implemented like ToDoModel in the tutorial exemple : https://github.com/mitchcurtis/todo-list-tutorial/tree/chapter-11
So the question is : how can I access from C++ the content of the LightModel objects because the 24x7 instances are created by .qml file.
Thanks a lot for your time
c++ qt qml
I have followed this tutorial : https://resources.qt.io/resources-by-content-type-videos-demos-tutorials/using-c-models-in-qml-tutorial
This was usefull to create my c++ class to manage my LightModel
Now I want to re-use this tutorial where I have a model inside 2 other repeaters in cascade. The issue is that my qml code instantiate LightModel objects inside repeaters so I don't have access to these instances from main.cpp
I used qmlRegisterType to give qml the ability to create several objects from my LightModel class.
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<Light>("Light",1,0,"Light");
qmlRegisterType<LightModel>("Light", 1,0,"LightModel");
qmlRegisterUncreatableType<Light>("Light", 1, 0, "LightList", QStringLiteral("LightList should not be created in QML"));
QQmlApplicationEngine engine;
MainWindow w;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
#include <QVector>
struct LightItem{
QString description;
int value;
int bornInf;
int bornSup;
int decimals; //1, 10
bool enabled;
};
class Light : public QObject
{
Q_OBJECT
public:
explicit Light(QObject *parent = nullptr);
QVector<LightItem> items() const;
bool setItemAt(int index, const LightItem &item);
signals:
void preItemAppended();
void postItemAppended();
void preItemRemoved(int index);
void postItemRemoved();
private:
QVector<LightItem> mItems;
};
#endif // LIGHT_H
lightmodel.h
#ifndef LIGHTMODEL_H
#define LIGHTMODEL_H
#include <QAbstractListModel>
class Light;
class LightModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(Light *list READ list WRITE setList)
Q_PROPERTY(int ledMode READ getLedMode WRITE setLedMode)
public:
explicit LightModel(QObject *parent = nullptr);
enum{
DescriptionRole = Qt::UserRole,
ValueRole,
BornInfRole,
BornSupRole,
Decimals,
EnableRole
};
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual QHash<int, QByteArray> roleNames() const override;
Light *list() const;
void setList(Light *list);
int getLedMode() const;
void setLedMode(int value);
private:
Light *mList;
int ledMode;
};
#endif // LIGHTMODEL_H
main.qml
StackLayout {
id: lampChose
currentIndex: gridLamps.indexImage
Repeater {
id: repeater1
model: 24
StackLayout {
id: lampStack
currentIndex: tabBarNm.currentIndex
Repeater {
id: repeater2
model: 7
Column {
id: columnSpinBoxtConfLeds
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: LightModel {
id: lightModel
list: light }
SpinBox {
id: spinBoxTest
visible: true
editable: true
value: model.value
from: model.bornInf
to: model.bornSup
stepSize: 1
onValueChanged: {
model.value = value
}
}
}
}
}
}
}
My LightModel class is implemented like ToDoModel in the tutorial exemple : https://github.com/mitchcurtis/todo-list-tutorial/tree/chapter-11
So the question is : how can I access from C++ the content of the LightModel objects because the 24x7 instances are created by .qml file.
Thanks a lot for your time
c++ qt qml
c++ qt qml
edited Nov 14 '18 at 13:41
asked Nov 14 '18 at 10:11
C. Duval
12
12
Hi there, welcome to Stack Overflow! I really appreciate the inclusion of code and background of your issue. What is slightly bugging me, is the question at the end.how can I access
. Can you clarify here? Access what? Access how? Individual objects ofLightModel
? I would love it if you could clarify this. Thanks!
– TrebuchetMS
Nov 14 '18 at 10:19
The easiest solution (but maybe not the best) would be to register eachLightModel
from the constructor to theLightList
. But it really depends on why you want to access the Lights. From theid
it seems you are trying to create some sort of shop-gallery?
– Amfasis
Nov 14 '18 at 10:35
Hi, thank you both for your answer, @TrebuchetMS I would like to have the reference on the created objects because I want to do backend stuff with it. For exemple, this qml file create 24x7 instance of LightModel class, the user modify theses instances with the HMI, but I don't manage to reach these instances from c++. I don't know if you understand what I mean ?
– C. Duval
Nov 14 '18 at 10:44
@Amfasis, this will take a lot of lines in c++ file and a huge amount of lines in .qml file to register instances one by one and keep trace of them that way I think...
– C. Duval
Nov 14 '18 at 10:44
1
Hi, I update the post to integrate my LightModel.h and Light.h The updates are working fine because i can log and see model.value changing, but I have no way to see it from main.cpp In other words : i would like to correlate the instances LightModel created 24x7 times and identify each one with both index of repeater1 and repeater2 (generated by "model :24 and model:7") At the end I would like to have for example an array or a vector like :LightModel mArray[24][7]
which reference all the instances by index them.
– C. Duval
Nov 14 '18 at 13:01
|
show 7 more comments
Hi there, welcome to Stack Overflow! I really appreciate the inclusion of code and background of your issue. What is slightly bugging me, is the question at the end.how can I access
. Can you clarify here? Access what? Access how? Individual objects ofLightModel
? I would love it if you could clarify this. Thanks!
– TrebuchetMS
Nov 14 '18 at 10:19
The easiest solution (but maybe not the best) would be to register eachLightModel
from the constructor to theLightList
. But it really depends on why you want to access the Lights. From theid
it seems you are trying to create some sort of shop-gallery?
– Amfasis
Nov 14 '18 at 10:35
Hi, thank you both for your answer, @TrebuchetMS I would like to have the reference on the created objects because I want to do backend stuff with it. For exemple, this qml file create 24x7 instance of LightModel class, the user modify theses instances with the HMI, but I don't manage to reach these instances from c++. I don't know if you understand what I mean ?
– C. Duval
Nov 14 '18 at 10:44
@Amfasis, this will take a lot of lines in c++ file and a huge amount of lines in .qml file to register instances one by one and keep trace of them that way I think...
– C. Duval
Nov 14 '18 at 10:44
1
Hi, I update the post to integrate my LightModel.h and Light.h The updates are working fine because i can log and see model.value changing, but I have no way to see it from main.cpp In other words : i would like to correlate the instances LightModel created 24x7 times and identify each one with both index of repeater1 and repeater2 (generated by "model :24 and model:7") At the end I would like to have for example an array or a vector like :LightModel mArray[24][7]
which reference all the instances by index them.
– C. Duval
Nov 14 '18 at 13:01
Hi there, welcome to Stack Overflow! I really appreciate the inclusion of code and background of your issue. What is slightly bugging me, is the question at the end.
how can I access
. Can you clarify here? Access what? Access how? Individual objects of LightModel
? I would love it if you could clarify this. Thanks!– TrebuchetMS
Nov 14 '18 at 10:19
Hi there, welcome to Stack Overflow! I really appreciate the inclusion of code and background of your issue. What is slightly bugging me, is the question at the end.
how can I access
. Can you clarify here? Access what? Access how? Individual objects of LightModel
? I would love it if you could clarify this. Thanks!– TrebuchetMS
Nov 14 '18 at 10:19
The easiest solution (but maybe not the best) would be to register each
LightModel
from the constructor to the LightList
. But it really depends on why you want to access the Lights. From the id
it seems you are trying to create some sort of shop-gallery?– Amfasis
Nov 14 '18 at 10:35
The easiest solution (but maybe not the best) would be to register each
LightModel
from the constructor to the LightList
. But it really depends on why you want to access the Lights. From the id
it seems you are trying to create some sort of shop-gallery?– Amfasis
Nov 14 '18 at 10:35
Hi, thank you both for your answer, @TrebuchetMS I would like to have the reference on the created objects because I want to do backend stuff with it. For exemple, this qml file create 24x7 instance of LightModel class, the user modify theses instances with the HMI, but I don't manage to reach these instances from c++. I don't know if you understand what I mean ?
– C. Duval
Nov 14 '18 at 10:44
Hi, thank you both for your answer, @TrebuchetMS I would like to have the reference on the created objects because I want to do backend stuff with it. For exemple, this qml file create 24x7 instance of LightModel class, the user modify theses instances with the HMI, but I don't manage to reach these instances from c++. I don't know if you understand what I mean ?
– C. Duval
Nov 14 '18 at 10:44
@Amfasis, this will take a lot of lines in c++ file and a huge amount of lines in .qml file to register instances one by one and keep trace of them that way I think...
– C. Duval
Nov 14 '18 at 10:44
@Amfasis, this will take a lot of lines in c++ file and a huge amount of lines in .qml file to register instances one by one and keep trace of them that way I think...
– C. Duval
Nov 14 '18 at 10:44
1
1
Hi, I update the post to integrate my LightModel.h and Light.h The updates are working fine because i can log and see model.value changing, but I have no way to see it from main.cpp In other words : i would like to correlate the instances LightModel created 24x7 times and identify each one with both index of repeater1 and repeater2 (generated by "model :24 and model:7") At the end I would like to have for example an array or a vector like :
LightModel mArray[24][7]
which reference all the instances by index them.– C. Duval
Nov 14 '18 at 13:01
Hi, I update the post to integrate my LightModel.h and Light.h The updates are working fine because i can log and see model.value changing, but I have no way to see it from main.cpp In other words : i would like to correlate the instances LightModel created 24x7 times and identify each one with both index of repeater1 and repeater2 (generated by "model :24 and model:7") At the end I would like to have for example an array or a vector like :
LightModel mArray[24][7]
which reference all the instances by index them.– C. Duval
Nov 14 '18 at 13:01
|
show 7 more comments
1 Answer
1
active
oldest
votes
I've found the solution to my problem : stop using repeaters with StackLayout.
My editable content is stored inside an array, set as context property inside my main.cpp and the view get the value corresponding to the current index
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
class LightV2 : public QObject
{
Q_OBJECT
public:
explicit LightV2(QObject *parent = nullptr);
public slots:
Q_INVOKABLE int getArrayValue(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornInf(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornSup(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getDecimal(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE void setArrayValue(int indexLamp, int indexLight, int indexField, int value);
private:
QString mDescription;
int mValue;
int mArrayValue[24][7][6];
int mBornInf[24][7][6];
int mBornSup[24][7][6];
int mDecimal[24][7][6];
};
#endif // LIGHTV2_H
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<GeneralConf>("Light", 1,0, "GeneralConf");
QQmlApplicationEngine engine;
LightV2 lights;
MainWindow w;
engine.rootContext()->setContextProperty("lights", &lights);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
finally : solution in main.qml
Column {
id: columnSpinBoxtConfLeds
x:110
y:0
width:200
spacing: 10
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: 6
SpinBox {
id: spinBoxTest
property int decimals: lights.getDecimal(gridLamps.indexImage,tabBarNm.currentIndex, model.index);
property int decimalPresent:{
if (decimals == 10){
1
}
else{
0
}
}
width : 150
height: 25
focusPolicy: Qt.TabFocus
wheelEnabled: false
visible: true
editable: true
enabled: {
console.log("spinbox index:",model.index)
if (model.index == 2 || model.index == 3){
if(comboBoxMode.currentIndex >=1){
1
}
else{
0
}
}
else if (model.index >= 4){
if(comboBoxMode.currentIndex >=2){
1
}
else{
0
}
}
else{
1
}
}
value: lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
from : lights.getBornInf(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
to : lights.getBornSup(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
stepSize: 1
textFromValue: function(value, locale){
return Number(value / decimals).toLocaleString(locale, 'f',decimalPresent)
}
valueFromText: function(text,locale){
return Number.fromLocaleString(locale, text) * decimals
}
onValueChanged: {
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
console.log(lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index));
}
}
to conclude : this is how I managed to resolve my issue :
lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
and
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297679%2fhow-can-i-use-model-c-class-in-cascade-with-2-other-repeater-in-qml-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've found the solution to my problem : stop using repeaters with StackLayout.
My editable content is stored inside an array, set as context property inside my main.cpp and the view get the value corresponding to the current index
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
class LightV2 : public QObject
{
Q_OBJECT
public:
explicit LightV2(QObject *parent = nullptr);
public slots:
Q_INVOKABLE int getArrayValue(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornInf(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornSup(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getDecimal(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE void setArrayValue(int indexLamp, int indexLight, int indexField, int value);
private:
QString mDescription;
int mValue;
int mArrayValue[24][7][6];
int mBornInf[24][7][6];
int mBornSup[24][7][6];
int mDecimal[24][7][6];
};
#endif // LIGHTV2_H
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<GeneralConf>("Light", 1,0, "GeneralConf");
QQmlApplicationEngine engine;
LightV2 lights;
MainWindow w;
engine.rootContext()->setContextProperty("lights", &lights);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
finally : solution in main.qml
Column {
id: columnSpinBoxtConfLeds
x:110
y:0
width:200
spacing: 10
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: 6
SpinBox {
id: spinBoxTest
property int decimals: lights.getDecimal(gridLamps.indexImage,tabBarNm.currentIndex, model.index);
property int decimalPresent:{
if (decimals == 10){
1
}
else{
0
}
}
width : 150
height: 25
focusPolicy: Qt.TabFocus
wheelEnabled: false
visible: true
editable: true
enabled: {
console.log("spinbox index:",model.index)
if (model.index == 2 || model.index == 3){
if(comboBoxMode.currentIndex >=1){
1
}
else{
0
}
}
else if (model.index >= 4){
if(comboBoxMode.currentIndex >=2){
1
}
else{
0
}
}
else{
1
}
}
value: lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
from : lights.getBornInf(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
to : lights.getBornSup(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
stepSize: 1
textFromValue: function(value, locale){
return Number(value / decimals).toLocaleString(locale, 'f',decimalPresent)
}
valueFromText: function(text,locale){
return Number.fromLocaleString(locale, text) * decimals
}
onValueChanged: {
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
console.log(lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index));
}
}
to conclude : this is how I managed to resolve my issue :
lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
and
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
add a comment |
I've found the solution to my problem : stop using repeaters with StackLayout.
My editable content is stored inside an array, set as context property inside my main.cpp and the view get the value corresponding to the current index
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
class LightV2 : public QObject
{
Q_OBJECT
public:
explicit LightV2(QObject *parent = nullptr);
public slots:
Q_INVOKABLE int getArrayValue(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornInf(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornSup(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getDecimal(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE void setArrayValue(int indexLamp, int indexLight, int indexField, int value);
private:
QString mDescription;
int mValue;
int mArrayValue[24][7][6];
int mBornInf[24][7][6];
int mBornSup[24][7][6];
int mDecimal[24][7][6];
};
#endif // LIGHTV2_H
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<GeneralConf>("Light", 1,0, "GeneralConf");
QQmlApplicationEngine engine;
LightV2 lights;
MainWindow w;
engine.rootContext()->setContextProperty("lights", &lights);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
finally : solution in main.qml
Column {
id: columnSpinBoxtConfLeds
x:110
y:0
width:200
spacing: 10
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: 6
SpinBox {
id: spinBoxTest
property int decimals: lights.getDecimal(gridLamps.indexImage,tabBarNm.currentIndex, model.index);
property int decimalPresent:{
if (decimals == 10){
1
}
else{
0
}
}
width : 150
height: 25
focusPolicy: Qt.TabFocus
wheelEnabled: false
visible: true
editable: true
enabled: {
console.log("spinbox index:",model.index)
if (model.index == 2 || model.index == 3){
if(comboBoxMode.currentIndex >=1){
1
}
else{
0
}
}
else if (model.index >= 4){
if(comboBoxMode.currentIndex >=2){
1
}
else{
0
}
}
else{
1
}
}
value: lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
from : lights.getBornInf(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
to : lights.getBornSup(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
stepSize: 1
textFromValue: function(value, locale){
return Number(value / decimals).toLocaleString(locale, 'f',decimalPresent)
}
valueFromText: function(text,locale){
return Number.fromLocaleString(locale, text) * decimals
}
onValueChanged: {
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
console.log(lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index));
}
}
to conclude : this is how I managed to resolve my issue :
lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
and
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
add a comment |
I've found the solution to my problem : stop using repeaters with StackLayout.
My editable content is stored inside an array, set as context property inside my main.cpp and the view get the value corresponding to the current index
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
class LightV2 : public QObject
{
Q_OBJECT
public:
explicit LightV2(QObject *parent = nullptr);
public slots:
Q_INVOKABLE int getArrayValue(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornInf(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornSup(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getDecimal(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE void setArrayValue(int indexLamp, int indexLight, int indexField, int value);
private:
QString mDescription;
int mValue;
int mArrayValue[24][7][6];
int mBornInf[24][7][6];
int mBornSup[24][7][6];
int mDecimal[24][7][6];
};
#endif // LIGHTV2_H
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<GeneralConf>("Light", 1,0, "GeneralConf");
QQmlApplicationEngine engine;
LightV2 lights;
MainWindow w;
engine.rootContext()->setContextProperty("lights", &lights);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
finally : solution in main.qml
Column {
id: columnSpinBoxtConfLeds
x:110
y:0
width:200
spacing: 10
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: 6
SpinBox {
id: spinBoxTest
property int decimals: lights.getDecimal(gridLamps.indexImage,tabBarNm.currentIndex, model.index);
property int decimalPresent:{
if (decimals == 10){
1
}
else{
0
}
}
width : 150
height: 25
focusPolicy: Qt.TabFocus
wheelEnabled: false
visible: true
editable: true
enabled: {
console.log("spinbox index:",model.index)
if (model.index == 2 || model.index == 3){
if(comboBoxMode.currentIndex >=1){
1
}
else{
0
}
}
else if (model.index >= 4){
if(comboBoxMode.currentIndex >=2){
1
}
else{
0
}
}
else{
1
}
}
value: lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
from : lights.getBornInf(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
to : lights.getBornSup(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
stepSize: 1
textFromValue: function(value, locale){
return Number(value / decimals).toLocaleString(locale, 'f',decimalPresent)
}
valueFromText: function(text,locale){
return Number.fromLocaleString(locale, text) * decimals
}
onValueChanged: {
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
console.log(lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index));
}
}
to conclude : this is how I managed to resolve my issue :
lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
and
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
I've found the solution to my problem : stop using repeaters with StackLayout.
My editable content is stored inside an array, set as context property inside my main.cpp and the view get the value corresponding to the current index
light.h
#ifndef LIGHT_H
#define LIGHT_H
#include <QObject>
class LightV2 : public QObject
{
Q_OBJECT
public:
explicit LightV2(QObject *parent = nullptr);
public slots:
Q_INVOKABLE int getArrayValue(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornInf(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getBornSup(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE int getDecimal(int indexLamp, int indexLight, int indexField);
Q_INVOKABLE void setArrayValue(int indexLamp, int indexLight, int indexField, int value);
private:
QString mDescription;
int mValue;
int mArrayValue[24][7][6];
int mBornInf[24][7][6];
int mBornSup[24][7][6];
int mDecimal[24][7][6];
};
#endif // LIGHTV2_H
main.cpp
int main(int argc, char *argv)
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
qmlRegisterType<GeneralConf>("Light", 1,0, "GeneralConf");
QQmlApplicationEngine engine;
LightV2 lights;
MainWindow w;
engine.rootContext()->setContextProperty("lights", &lights);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
w.show();
return app.exec();
}
finally : solution in main.qml
Column {
id: columnSpinBoxtConfLeds
x:110
y:0
width:200
spacing: 10
Repeater {
id: repeaterParametersSpinBoxesLEDs
model: 6
SpinBox {
id: spinBoxTest
property int decimals: lights.getDecimal(gridLamps.indexImage,tabBarNm.currentIndex, model.index);
property int decimalPresent:{
if (decimals == 10){
1
}
else{
0
}
}
width : 150
height: 25
focusPolicy: Qt.TabFocus
wheelEnabled: false
visible: true
editable: true
enabled: {
console.log("spinbox index:",model.index)
if (model.index == 2 || model.index == 3){
if(comboBoxMode.currentIndex >=1){
1
}
else{
0
}
}
else if (model.index >= 4){
if(comboBoxMode.currentIndex >=2){
1
}
else{
0
}
}
else{
1
}
}
value: lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
from : lights.getBornInf(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
to : lights.getBornSup(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
stepSize: 1
textFromValue: function(value, locale){
return Number(value / decimals).toLocaleString(locale, 'f',decimalPresent)
}
valueFromText: function(text,locale){
return Number.fromLocaleString(locale, text) * decimals
}
onValueChanged: {
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
console.log(lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index));
}
}
to conclude : this is how I managed to resolve my issue :
lights.getArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index)
and
lights.setArrayValue(gridLamps.indexImage,tabBarNm.currentIndex, model.index, value);
answered Nov 15 '18 at 8:30
C. Duval
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297679%2fhow-can-i-use-model-c-class-in-cascade-with-2-other-repeater-in-qml-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi there, welcome to Stack Overflow! I really appreciate the inclusion of code and background of your issue. What is slightly bugging me, is the question at the end.
how can I access
. Can you clarify here? Access what? Access how? Individual objects ofLightModel
? I would love it if you could clarify this. Thanks!– TrebuchetMS
Nov 14 '18 at 10:19
The easiest solution (but maybe not the best) would be to register each
LightModel
from the constructor to theLightList
. But it really depends on why you want to access the Lights. From theid
it seems you are trying to create some sort of shop-gallery?– Amfasis
Nov 14 '18 at 10:35
Hi, thank you both for your answer, @TrebuchetMS I would like to have the reference on the created objects because I want to do backend stuff with it. For exemple, this qml file create 24x7 instance of LightModel class, the user modify theses instances with the HMI, but I don't manage to reach these instances from c++. I don't know if you understand what I mean ?
– C. Duval
Nov 14 '18 at 10:44
@Amfasis, this will take a lot of lines in c++ file and a huge amount of lines in .qml file to register instances one by one and keep trace of them that way I think...
– C. Duval
Nov 14 '18 at 10:44
1
Hi, I update the post to integrate my LightModel.h and Light.h The updates are working fine because i can log and see model.value changing, but I have no way to see it from main.cpp In other words : i would like to correlate the instances LightModel created 24x7 times and identify each one with both index of repeater1 and repeater2 (generated by "model :24 and model:7") At the end I would like to have for example an array or a vector like :
LightModel mArray[24][7]
which reference all the instances by index them.– C. Duval
Nov 14 '18 at 13:01