passing Q_GADGET object as argument
-
Hi all -
I tried to code a C++ routine that would be invoked from QML. One of the arguments is a struct I've written that uses Q_GADGET. When I try to pass this by reference, I get the following errors:
static assertion failed: Meta Types cannot be non-const references or rvalue references. forming pointer to reference type 'Equipment&'
I did some searching, and I think I understand the reason. My question: does the second error message I got suggest that Qt is creating a pointer for me (which would render my attempt superfluous)?
The called function does need to modify the contents of this struct, so this has implications for what to do if the answer is "no."
Thanks...
-
Hi all -
I tried to code a C++ routine that would be invoked from QML. One of the arguments is a struct I've written that uses Q_GADGET. When I try to pass this by reference, I get the following errors:
static assertion failed: Meta Types cannot be non-const references or rvalue references. forming pointer to reference type 'Equipment&'
I did some searching, and I think I understand the reason. My question: does the second error message I got suggest that Qt is creating a pointer for me (which would render my attempt superfluous)?
The called function does need to modify the contents of this struct, so this has implications for what to do if the answer is "no."
Thanks...
@mzimmers said in passing Q_GADGET object as argument:
The called function does need to modify the contents of this struct,
So fix this design flaw - signals and slots are not meant to modify the passsed function so the calling function (signal) is using the modified object afterwards. It can not work since there is e.g. Qt::QueuedConnections.
-
@mzimmers said in passing Q_GADGET object as argument:
The called function does need to modify the contents of this struct,
So fix this design flaw - signals and slots are not meant to modify the passsed function so the calling function (signal) is using the modified object afterwards. It can not work since there is e.g. Qt::QueuedConnections.
@Christian-Ehrlicher I'm not using signals and slots - this is a Q_GADGET, not a Q_OBJECT. I merely coded a Q_INVOKABLE routine that is called from the QML, to modify the model contents.
-
@Christian-Ehrlicher I'm not using signals and slots - this is a Q_GADGET, not a Q_OBJECT. I merely coded a Q_INVOKABLE routine that is called from the QML, to modify the model contents.
@mzimmers said in passing Q_GADGET object as argument:
merely coded a Q_INVOKABLE routine that is called from the QML,
This is not different from a signal slot connection. Fix your design.
-
@mzimmers said in passing Q_GADGET object as argument:
merely coded a Q_INVOKABLE routine that is called from the QML,
This is not different from a signal slot connection. Fix your design.
@Christian-Ehrlicher what is wrong with a design that invokes C++ methods to operate on data used in QML?
-
@Christian-Ehrlicher what is wrong with a design that invokes C++ methods to operate on data used in QML?
@mzimmers
Do I get this right: The intention is to pass a function pointer as an argument, and you want to use the macros designed for signal/slot connections to derive that pointer from a string? It doesn’t work, because the macros haven’t been designed for that purpose. It would be possible to plumb that functionality around QMetaObject. But that’ll process a string at runtime, while you could use the function pointer directly. What’s the reason for discarding that way? -
@mzimmers
Do I get this right: The intention is to pass a function pointer as an argument, and you want to use the macros designed for signal/slot connections to derive that pointer from a string? It doesn’t work, because the macros haven’t been designed for that purpose. It would be possible to plumb that functionality around QMetaObject. But that’ll process a string at runtime, while you could use the function pointer directly. What’s the reason for discarding that way?@Axel-Spoerl no, not at all. Maybe some code will help. I have a struct:
struct Equipment { Q_GADGET QML_STRUCTURED_VALUE QML_VALUE_TYPE(equipment) ...
and I maintain a list of these in a model.
In my QML, I have a ListView that accesses the model, and furnishes the contents to a delegate that eventually gets down to:
equipmentModel.processSpaceAssignment(vspInModel, spaceUuid, checked)
where vspInModel is an instance of a subclass of Equipment. There's some rather complex work that needs to be done, so I didn't want to try to implement it in JS. The C++ function signature is:
bool EquipmentModel::processSpaceAssignment(Equipment equipment, QUuid spaceUuid, bool add)
So, my original question was, is this equipment/vsp object passed by value in this case?