Correct syntax to register object reference using qRegisterMetaType?
-
This is still an issue, I am passing references to signals, type: QJsonObject&, I found this post:
https://stackoverflow.com/questions/23219770/how-to-register-reference-to-qt-containersHowever it hasn't helped, I added a call to:
qRegisterMetaType<QJsonObject>("QJsonObject");
I'm still getting:
QObject::connect: Cannot queue arguments of type 'QJsonObject&'
Note I haven't put in the & in the registration as the post on stackoverflow advised against it.
As far as I can see the code is working regardless of the warning, I just don't like messages like this because they suggest something is wrong, can I disable the warning?
Where I have these types the connects have been modified having the last parameter as , Qt::DirectConnection
-
Hi,
Don't pass references in parameters. As @JKSH wrote earlier, most of Qt datatypes are implicitly shared so there's really no need to try to go that way.
If you are forcing the Qt::DirectConnection because of that then it means you have a design issue. It's currently pretty rare to have to do that and it also has consequences when doing multithreading.
-
@SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:
I want to pass by reference because my understanding of passing by reference is that it is far less costly on stack space. A QJsonObject can be very large.
Your understanding is irrelevant in this case. You were told it isn't possible with a queued connection, and it still isn't, and it is not going to be, no matter how much we argue about it. The warning is there for a reason - to warn you that you have a serious bug in your code. You can either use a direct connection and suffer the inevitable consequences of that, or use
const QJsonObject &
/QJsonObject
for your signal arguments. -
@kshegunov , I am using const QJsonObject& for my signal arguments, these are working, the only thing that is displayed in the Application Output is the message I've raised. I'm not experience any other problems, crashes or bugs.
-
@SPlatten
The signal argument type is "OK", insofar as it would work OK with non-cross-thread direct connectionconnect()
s. But the problem is that you are trying to use it when you need a queued connection. That is why you get the error message on theQObject::connect
statement. -
@SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:
@kshegunov , I am using const QJsonObject& for my signal arguments, these are working, the only thing that is displayed in the Application Output is the message I've raised. I'm not experience any other problems, crashes or bugs.
For the slots as well ?
-
@SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:
not for the ones where I've used Qt::DirectConnection
Then it's useless since then Qt::AutoConnection evaluates to Qt::DirectConnection. Specifying a connection type is not needed in most of the cases.
-
@Christian-Ehrlicher , Well that's the only change I'm implemented and now there is no message, so perhaps this is something that needs looking at?
-
@SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:
@Christian-Ehrlicher , Well that's the only change I'm implemented and now there is no message, so perhaps this is something that needs looking at?
For that a minimal compilable example would be needed.
-
@SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:
so perhaps this is something that needs looking at?
This means that there is a thread context switch somewhere even you tell us it isn't.
-
@SPlatten said in Correct syntax to register object reference using qRegisterMetaType?:
@Christian-Ehrlicher , Well that's the only change I'm implemented and now there is no message, so perhaps this is something that needs looking at?
Run with
QT_FATAL_WARNINGS=1
as an environment variable and extract the stack trace, then you will have leveraged your debugger to show you exactly where you're passing a reference between threads. -
@kshegunov said in Correct syntax to register object reference using qRegisterMetaType?:
QT_FATAL_WARNINGS=1
I've opened both .pro files and added that line, then cleaned and rebuilt, the applications have been running for several minutes, nothing to report, no issues.
-
It's an environnement variable, not a define.
You have to set it in the Run tab of the Project panel.