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. QJsonArray: Reordering the items
Forum Update on Tuesday, May 27th 2025

QJsonArray: Reordering the items

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 350 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 29 Aug 2019, 16:29 last edited by fem_dev
    #1

    Hi, my QJsonArray example have only 3 QJsonObject items:

    [
        {
            "id": 0,
            "name": "Nyck"
        },
        {
            "id": 1,
            "name": "Paul"
        },
        {
            "id": 2,
            "name": "Raul"
        }
    ]
    

    How can I reorder these QJsonObjects passing the current element index and the future element index?
    I would like to obtain something like this:

    my_array->magic_reorder(2, 0) // This is a "fake" example
    

    The result should be:

    [
        {
            "id": 2,
            "name": "Raul"
        },
        {
            "id": 0,
            "name": "Nyck"
        },
        {
            "id": 1,
            "name": "Paul"
        }
    ]
    

    How can I do this?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 29 Aug 2019, 16:48 last edited by
      #2

      Since it's a QJsonArray you should take a look at it's functions - esp. at(), operator[] and maybe removeAt()

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      F 1 Reply Last reply 29 Aug 2019, 17:19
      3
      • C Christian Ehrlicher
        29 Aug 2019, 16:48

        Since it's a QJsonArray you should take a look at it's functions - esp. at(), operator[] and maybe removeAt()

        F Offline
        F Offline
        fem_dev
        wrote on 29 Aug 2019, 17:19 last edited by
        #3

        @christian-ehrlicher thank you!

        I got it working now using:

         my_array->replace(target_id, current_json);
        
        1 Reply Last reply
        0
        • F Offline
          F Offline
          fem_dev
          wrote on 29 Aug 2019, 17:46 last edited by
          #4

          I solved this problem and post a generic solution here:
          https://forum.qt.io/topic/106344/qlistwidget-dragdrop-event-signal/5

          1 Reply Last reply
          0

          1/4

          29 Aug 2019, 16:29

          • Login

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