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. Minimal example to drag-reorder rows in QTableView?
Forum Updated to NodeBB v4.3 + New Features

Minimal example to drag-reorder rows in QTableView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 1.6k Views 3 Watching
  • 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by
    #2

    I'm afraid the problem is that none of Qt's models implement moving rows so you'll have to code the behaviour manually

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    1 Reply Last reply
    1
    • P patrickkidd

      I am having a hard time figuring out how to reorder rows in my QTableView with a custom model. It uses SelectRows selection behavior:

      self.setSelectionBehavior(QAbstractItemView.SelectRows)
      

      Does anyone have or know of a minimal example to get this to work? I am starting to get a little frustrated with repeated failures.

      Thanks!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #3

      @patrickkidd
      Although as @VRonin comments Qt's models do not implement moving rows, and that would mean you'd have to roll your own code completely, since you say you are using a custom model you could/might be able to implement:

      • Include a column for "row number" in your model, if you can do that (if you can't, no dice). Note that this is what would be required if your model were a database and you needed to impose a row order for some purpose.

      • When a row is dragged in the view to reorder, you must implement the updating of that row's row number, plus all other rows after it which will will also need re-numbering.

      • You can now use, for example, a QSortFilterProxy between model & view which sorts by the model's row number column to implement reordering.

      Whether this is easy/appropriate for your model/view only you will know.

      kshegunovK P 2 Replies Last reply
      0
      • JonBJ JonB

        @patrickkidd
        Although as @VRonin comments Qt's models do not implement moving rows, and that would mean you'd have to roll your own code completely, since you say you are using a custom model you could/might be able to implement:

        • Include a column for "row number" in your model, if you can do that (if you can't, no dice). Note that this is what would be required if your model were a database and you needed to impose a row order for some purpose.

        • When a row is dragged in the view to reorder, you must implement the updating of that row's row number, plus all other rows after it which will will also need re-numbering.

        • You can now use, for example, a QSortFilterProxy between model & view which sorts by the model's row number column to implement reordering.

        Whether this is easy/appropriate for your model/view only you will know.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #4

        Isn't it simpler to just make a reorder proxy model and put it over the original model, and then put the sort-filter proxy on top of it?
        It's kind of the reasons proxies exist in the first place.

        Read and abide by the Qt Code of Conduct

        JonBJ 1 Reply Last reply
        1
        • kshegunovK kshegunov

          Isn't it simpler to just make a reorder proxy model and put it over the original model, and then put the sort-filter proxy on top of it?
          It's kind of the reasons proxies exist in the first place.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #5

          @kshegunov
          It might be, if we (I) knew what a "a reorder proxy model" was, or you provided a link to it.... :)

          kshegunovK 1 Reply Last reply
          0
          • JonBJ JonB

            @kshegunov
            It might be, if we (I) knew what a "a reorder proxy model" was, or you provided a link to it.... :)

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #6

            That'd be your proxy model derived from the identity proxy.

            Read and abide by the Qt Code of Conduct

            JonBJ 1 Reply Last reply
            0
            • kshegunovK kshegunov

              That'd be your proxy model derived from the identity proxy.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #7

              @kshegunov
              And how/where would this proxy model I'm going to write store the information on what the desired order is, please?

              kshegunovK 1 Reply Last reply
              0
              • JonBJ JonB

                @kshegunov
                And how/where would this proxy model I'm going to write store the information on what the desired order is, please?

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #8

                @JonB said in Minimal example to drag-reorder rows in QTableView?:

                And how/where would this proxy model I'm going to write store the information on what the desired order is, please?

                Inside the proxy model itself; you provide mapping between the source and destination model indices similarly to how the sort-filter proxy does it.

                Read and abide by the Qt Code of Conduct

                JonBJ 1 Reply Last reply
                0
                • kshegunovK kshegunov

                  @JonB said in Minimal example to drag-reorder rows in QTableView?:

                  And how/where would this proxy model I'm going to write store the information on what the desired order is, please?

                  Inside the proxy model itself; you provide mapping between the source and destination model indices similarly to how the sort-filter proxy does it.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @kshegunov
                  Ah, I see, not an area I have looked at.

                  Presumably that would work provided OP does not want to permanently store the ordered items in a database (which may be the case). Without a permanent extra column for the order there will be nothing to restore the order on subsequent data re-read.

                  1 Reply Last reply
                  1
                  • JonBJ JonB

                    @patrickkidd
                    Although as @VRonin comments Qt's models do not implement moving rows, and that would mean you'd have to roll your own code completely, since you say you are using a custom model you could/might be able to implement:

                    • Include a column for "row number" in your model, if you can do that (if you can't, no dice). Note that this is what would be required if your model were a database and you needed to impose a row order for some purpose.

                    • When a row is dragged in the view to reorder, you must implement the updating of that row's row number, plus all other rows after it which will will also need re-numbering.

                    • You can now use, for example, a QSortFilterProxy between model & view which sorts by the model's row number column to implement reordering.

                    Whether this is easy/appropriate for your model/view only you will know.

                    P Offline
                    P Offline
                    patrickkidd
                    wrote on last edited by
                    #10

                    @JonB This is for implementing a list of layers as in photoshop, and stored in the document. So yes, an 'order' column will be necessary in the model.

                    It does help me to know that dragging and reordering entire rows is not supported by the QTableView/model code. So after my reading of the docs and qt source code, I am still a little lost at where to start writing this feature. For example, how deeply I need to re-write this functionality; which qt api parts to use and which to reimplement? Can you give a starting point?

                    https://alaskafamilysystems.com/

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      Hi,

                      It's model specific and its normal that Qt doesn't provide default implementation for them. That ensure that the model doesn't do anything behind the back. The AbstractItemModel class can't presume what type of structure it is going to be put on top of.

                      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
                      1

                      • Login

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