nlohmann JSON: Object has changed (signal)
-
wrote on 2 Jul 2020, 16:35 last edited by
I'm using a external JSON API called
nlohmann JSON
inside my Qt GUI Application.
Github link: https://github.com/nlohmann/jsonI would like to connect a JSON object to a class method in a way that, every time that this JSON object
has changed
I receive asignal
in a customslot
.Below I wrote a simple not working example just to illustrate what I would like to do:
void myFunc(nlohman::json& json) { // Update JSON object: json["newValue"] = 5; } void App::Init() { .... nlohman::json myJson; .... myJson["newValue"] = 0; // May be, some connect syntax that I don't know... connect(myJson, nlohman::json::someMethod, this, mySlot) myFunc(myJson); } void App::mySlot() { qDebug() << "myJson has changed!"; }
So, if I call the
App::Init()
method, I would like to see theqDebug()
output:myJson has changed!
Is there possible?
If yes, how can I do it?My system:
- Ubuntu 20.04
- Qt 5.15
- Qt Creator IDE 4.12.3
-
I'm using a external JSON API called
nlohmann JSON
inside my Qt GUI Application.
Github link: https://github.com/nlohmann/jsonI would like to connect a JSON object to a class method in a way that, every time that this JSON object
has changed
I receive asignal
in a customslot
.Below I wrote a simple not working example just to illustrate what I would like to do:
void myFunc(nlohman::json& json) { // Update JSON object: json["newValue"] = 5; } void App::Init() { .... nlohman::json myJson; .... myJson["newValue"] = 0; // May be, some connect syntax that I don't know... connect(myJson, nlohman::json::someMethod, this, mySlot) myFunc(myJson); } void App::mySlot() { qDebug() << "myJson has changed!"; }
So, if I call the
App::Init()
method, I would like to see theqDebug()
output:myJson has changed!
Is there possible?
If yes, how can I do it?My system:
- Ubuntu 20.04
- Qt 5.15
- Qt Creator IDE 4.12.3
wrote on 2 Jul 2020, 16:52 last edited by@fem_dev you may want to read the signal and slots documentation.
A key requirement for this feature to work is that your class derives from QObject and it has the Q_OBJECT macro in the definition (see small example from documentation)On the other hand, is a rather compelling reason to use such an external library for JSON when Qt framework does support JSON indeed.
-
I'm using a external JSON API called
nlohmann JSON
inside my Qt GUI Application.
Github link: https://github.com/nlohmann/jsonI would like to connect a JSON object to a class method in a way that, every time that this JSON object
has changed
I receive asignal
in a customslot
.Below I wrote a simple not working example just to illustrate what I would like to do:
void myFunc(nlohman::json& json) { // Update JSON object: json["newValue"] = 5; } void App::Init() { .... nlohman::json myJson; .... myJson["newValue"] = 0; // May be, some connect syntax that I don't know... connect(myJson, nlohman::json::someMethod, this, mySlot) myFunc(myJson); } void App::mySlot() { qDebug() << "myJson has changed!"; }
So, if I call the
App::Init()
method, I would like to see theqDebug()
output:myJson has changed!
Is there possible?
If yes, how can I do it?My system:
- Ubuntu 20.04
- Qt 5.15
- Qt Creator IDE 4.12.3
wrote on 2 Jul 2020, 16:52 last edited by@fem_dev said in nlohmann JSON: Object has changed (signal):
Is there possible?
I would vote for NO since it doesn't use Qt at all.
-
@fem_dev you may want to read the signal and slots documentation.
A key requirement for this feature to work is that your class derives from QObject and it has the Q_OBJECT macro in the definition (see small example from documentation)On the other hand, is a rather compelling reason to use such an external library for JSON when Qt framework does support JSON indeed.
wrote on 2 Jul 2020, 17:26 last edited by@Pablo-J-Rogina Thank you...
I know that Qt JSON support...
I'm just using this external JSON API because I'm developing a C++ library that will be bind with
PyBind11
library, which will enable to use my C++ library inside the Python environment.Pybind11: https://github.com/pybind/pybind11
When my C++ library function return a JSON object, the Pybind11 understand the
nlohmann JSON
object and convert it to a Pythondict
native type.So I can use JSON data between C++ Library and Python environment.
I can't do that using the Qt JSON API. Right?
-
@Pablo-J-Rogina Thank you...
I know that Qt JSON support...
I'm just using this external JSON API because I'm developing a C++ library that will be bind with
PyBind11
library, which will enable to use my C++ library inside the Python environment.Pybind11: https://github.com/pybind/pybind11
When my C++ library function return a JSON object, the Pybind11 understand the
nlohmann JSON
object and convert it to a Pythondict
native type.So I can use JSON data between C++ Library and Python environment.
I can't do that using the Qt JSON API. Right?
wrote on 2 Jul 2020, 17:27 last edited by@fem_dev said in nlohmann JSON: Object has changed (signal):
So I can use JSON data between C++ Library and Python environment.
I can't do that using the Qt JSON API. Right?I really cannot help with that, i.e. C++ and Python bindings...
-
wrote on 6 Jul 2021, 14:22 last edited by
it seems you will have to add some signal emitting in your nlohman::json::someMethod, nohow else...you need to add emit someSignal(); to your nlohman::json::someMethod, and add definition like public slots: void someSignal(); and only then you will be able to connect this signal to mySlot like connect(myJson, nlohman::json::someSignal, this, mySlot)
-
it seems you will have to add some signal emitting in your nlohman::json::someMethod, nohow else...you need to add emit someSignal(); to your nlohman::json::someMethod, and add definition like public slots: void someSignal(); and only then you will be able to connect this signal to mySlot like connect(myJson, nlohman::json::someSignal, this, mySlot)
@sprokuda hi, that is wrong. The nlohman::json is a powerful C++ json handling library. It does not need to be modified to add support for Qt signals and slots, it should rather be used to process JSON data and be used by a class that does the Qt part of the work.