List of QPairs or QHash to qml
-
Hi,
I need a list Q_PROPERTY containing pair values like (150 , 300) that i can expose to qml and access it likemyPropName[index].first and myPropName[index].second
what is the best solution for this please ?
I tryed :
Q_PROPERTY(QQmlPropertyMap programSizes READ get_programSizes NOTIFY programSizesChanged)
but im not sure this can be done, i have errors when trying to write accessor for it
i also try to put QPairs in QVariantList but could not do it
QPair<double, double> pairHeightWidth; pairHeightWidth.first= qAbs(progHeight) pairHeightWidth.second = qAbs(progWidth); /*QVariantList*/ m_progSizes << pairHeightWidth
-
did like this
Q_PROPERTY(QVariantList progSizes READ getProgSizes WRITE setProgSizes NOTIFY progSizesChanged) private: QVariantList m_progSizes ... ...{ QVariantMap pair; pair.insert("width",999); pair.insert("height",66); m_progSizes << pair; } ... property variant ps: parser.progSizes onPsChanged: { console.log("prog 0 heigh/width : " + ps[0].height + "/" + ps[0].width) }
@J-Hilk thanks for help
-
@LeLev
try it as a pure QVariantQ_PROPERTY(QVariant programSizes READ get_programSizes NOTIFY programSizesChanged)
with
QVariant get_programSizes(){QVariant::fromValue(m_programSizes);} -
@J.Hilk hi,
thank you.
I tryed like this :Q_PROPERTY(QVariant programSizes READ get_programSizes WRITE set_programSizes NOTIFY programSizesChanged) ... QPair<double, double> pairHeightWidth; pairHeightWidth.first = qAbs(progWidth); pairHeightWidth.second = qAbs(progHeight); m_progSizes << QVariant::fromValue(pairHeightWidth);
this works but when i pass this Q_PROPERTY to another object through SIGNAL/SLOT
QObject::connect(parser,&ProgramParser::programSizesChanged,this,&ThreadsafeParser::set_programSizes);
i have this output :
QObject::connect: Cannot queue arguments of type 'QVariant&'
(Make sure 'QVariant&' is registered using qRegisterMetaType().)How/where i have to register QVariant& please ?
-
making my signal arg const solved this error
void programSizesChanged(const QVariant &mp);
instead of
void programSizesChanged( QVariant &mp); -
@LeLev said in List of QPairs or QHash to qml:
How/where i have to register QVariant& please ?
you use qRegisterMetaType usually like this:
qRegisterMetaType<MyStruct>();
where you call it, doesn't matter much, just make sure that you call it before you make the first connection that uses the type
-
@J.Hilk thank you,
sorry i made an error in my previously pasted code,this dont work, is m_programSizes is a QVariant
QPair<double, double> pairHeightWidth; pairHeightWidth.first = qAbs(progWidth); pairHeightWidth.second = qAbs(progHeight); m_programSizes << QVariant::fromValue(pairHeightWidth);
-
this works :
m_programSizes.setValue(QVariant::fromValue(pairHeightWidth));
but how to read this qml side now ?
i only got
property variant progSizes: parser.programSizes onProgSizesChanged: { for (var i in progSizes ){ console.log(i) }
output
QVariant(QPair<double,double>, ) -
@LeLev
i will try this
https://forum.qt.io/topic/87558/qlist-of-qpair-in-qml -
@LeLev im not sure if you can, as first and second are not defined as properties., inside the Pair class
-
did like this
Q_PROPERTY(QVariantList progSizes READ getProgSizes WRITE setProgSizes NOTIFY progSizesChanged) private: QVariantList m_progSizes ... ...{ QVariantMap pair; pair.insert("width",999); pair.insert("height",66); m_progSizes << pair; } ... property variant ps: parser.progSizes onPsChanged: { console.log("prog 0 heigh/width : " + ps[0].height + "/" + ps[0].width) }
@J-Hilk thanks for help
10/10