Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

nlohmann JSON: Object has changed (signal)



  • I'm using a external JSON API called nlohmann JSON inside my Qt GUI Application.
    Github link: https://github.com/nlohmann/json

    I 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 a signal in a custom slot.

    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 the qDebug() 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


  • @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.



  • @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.



  • @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.



  • @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 Python dict 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?



  • @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...


Log in to reply