Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved Is it possible for a QTableView to accept a drop in the empty area below its last row?

    General and Desktop
    qtableview drag and drop
    2
    7
    3792
    Loading More Posts
    • 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.
    • Guy Gizmo
      Guy Gizmo last edited by Guy Gizmo

      I have a QTableView that accepts drops in order to rearrange and create new rows. So far, everything is working, except that if the user performs a drop in the area below the final row of the table, it doesn't accept the drop. What I'd like it to do in that case is draw an indicator as though the user was hovering over the lower half of the final row and then accept the drop, placing it after the last row.

      Since a picture is worth a thousand words:

      Here is what it looks like when the user drops an item in the lower half of the last row. An indicator appears correctly, and when the user releases the mouse, a new row is added to the table. This is what I want to happen if the user drops an item on to the QTableView in the area below the last row.

      Here is where I also want the user to be able to drop an item, but currently it does not work. Note the lack of indicator.

      So far I've tried subclassing QTableView and reimplementing dragMoveEvent() and dropEvent() methods in such a way that it calls into QTableView's version of these function, but I pass in a new QDragMoveEvent or QDropEvent with a position that is in the lower half of the last row. While it does draw an indicator like I want, it doesn't accept the drop so this method doesn't seem to work.

      I'm also aware I can set the stretchLastSection property to true for the vertical header of my QTableView, but this is unacceptable, as I need all the rows of my table view to be the same height.

      Is there any way to accomplish this? At this point I'm stumped!

      1 Reply Last reply Reply Quote 0
      • B
        bsomervi last edited by

        This is handled by QAbstractItemModel::dropMimeData. It gets passed row and column arguments of -1 when the drop is to the parent of the view items.

        Guy Gizmo 1 Reply Last reply Reply Quote 1
        • Guy Gizmo
          Guy Gizmo @bsomervi last edited by

          @bsomervi You are correct. I'm not sure how I missed that, as I've already implemented the dropMimeData method.

          Is there any way to get the table view to display a drop indicator in this case, like when the dropping between existing rows?

          B 1 Reply Last reply Reply Quote 0
          • B
            bsomervi @Guy Gizmo last edited by

            @Guy-Gizmo said:

            Is there any way to get the table view to display a drop indicator in this case, like when the dropping between existing rows?

            Return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled from QAbstractItemModel::flags(QModelIndex const& index) const where index is invalid.

            Guy Gizmo 1 Reply Last reply Reply Quote 1
            • Guy Gizmo
              Guy Gizmo @bsomervi last edited by

              @bsomervi I am already doing that, and while the cursor does change to show that the item can be dropped, it doesn't display the horizontal line indicator showing that it will be put below the last line of the table view.

              B 1 Reply Last reply Reply Quote 0
              • B
                bsomervi @Guy Gizmo last edited by

                @Guy-Gizmo said:

                @bsomervi I am already doing that, and while the cursor does change to show that the item can be dropped, it doesn't display the horizontal line indicator showing that it will be put below the last line of the table view.

                I do not think that is easy. The lines around/above items are drawn by the QStyle in drawPrimitive() when the primitive is PE_IndicatorItemViewItemDrop so that can be changed with a QProxyStyle but that mechanism is for view items. I can't see an easy way of indicating in the viewport below the items when dragging there.

                Guy Gizmo 1 Reply Last reply Reply Quote 1
                • Guy Gizmo
                  Guy Gizmo @bsomervi last edited by

                  @bsomervi Well, that's okay -- it's not that important that the indicator be there. Thanks for your help!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post