Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. nlohmann JSON: Object has changed (signal)
QtWS25 Last Chance

nlohmann JSON: Object has changed (signal)

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fem_dev
    wrote on last edited by
    #1

    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
    Pablo J. RoginaP B 2 Replies Last reply
    0
    • F fem_dev

      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
      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

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

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      F 1 Reply Last reply
      1
      • F fem_dev

        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
        B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #3

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

        1 Reply Last reply
        1
        • Pablo J. RoginaP Pablo J. Rogina

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

          F Offline
          F Offline
          fem_dev
          wrote on last edited by
          #4

          @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?

          Pablo J. RoginaP 1 Reply Last reply
          0
          • F fem_dev

            @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?

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

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

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sprokuda
              wrote on last edited by
              #6

              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)

              SGaistS 1 Reply Last reply
              0
              • S sprokuda

                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)

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved