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. QTreeView: dragging and dropping items with left mouse button?
QtWS25 Last Chance

QTreeView: dragging and dropping items with left mouse button?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 3.9k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on 31 May 2015, 14:01 last edited by
    #1

    I'm unable to get drag and drop working using the left mouse button in a QTreeView. It does work with RMB. But with the LMB the view just selects multiple items that I drag across.

    Here are my tree view properties:
    http://i.stack.imgur.com/NT07q.png

    So, how can I get dragging with a left mouse button to work?

    P. S. I have confirmed that with the settings depicted above the "Editable tree model" example demonstrates exactly the same behavior.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 31 May 2015, 20:48 last edited by
      #2

      Hi,

      AFAIK, this example doesn't activate the drag and drop feature of QTreeView.

      You have the documentation here

      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
      • V Offline
        V Offline
        Violet Giraffe
        wrote on 31 May 2015, 21:11 last edited by
        #3

        That's why I added "with the settings depicted above". I open the example, change those properties and observe the undesired behavior.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 31 May 2015, 22:12 last edited by
          #4

          That's not enough. The example also provides a custom model. The model should also provide the Qt::ItemIsDragEnabled item flag as needed if you want your items to be draggable

          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
          • V Offline
            V Offline
            Violet Giraffe
            wrote on 1 Jun 2015, 04:33 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • V Offline
              V Offline
              Violet Giraffe
              wrote on 1 Jun 2015, 04:38 last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • V Offline
                V Offline
                Violet Giraffe
                wrote on 1 Jun 2015, 09:33 last edited by Violet Giraffe 6 Jan 2015, 09:59
                #7

                Found what's breaking the drag and drop. It's my code that deselects an item that was single-clicked:

                void CFileListView::mousePressEvent(QMouseEvent *e)`
                {
                	const QModelIndex itemClicked = indexAt(e->pos());
                
                    QTreeView::mousePressEvent(e);
                
                    if (e->button() == Qt::LeftButton && itemClicked.isValid() && e->modifiers() == Qt::NoModifier)
                	    selectionModel()->clearSelection();
                }
                

                If I comment out the clearSelection call, drag and drop starts working. With clearSelection it's as if D&D is not enabled.
                No idea what exactly is happening or how to fix this.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 1 Jun 2015, 23:09 last edited by
                  #8

                  To start a drag operation you need to select an index. What you do is that you clear that selection right after it happened so there's nothing to drag anymore.

                  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
                  • V Offline
                    V Offline
                    Violet Giraffe
                    wrote on 2 Jun 2015, 04:50 last edited by
                    #9

                    I see!
                    Can I somehow start drag manually in the mouseMove event?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 2 Jun 2015, 22:32 last edited by
                      #10

                      You would need to re-implement it yourself but why do you do that clear in the pressEvent ?

                      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
                      • V Offline
                        V Offline
                        Violet Giraffe
                        wrote on 3 Jun 2015, 05:00 last edited by Violet Giraffe 6 Mar 2015, 05:00
                        #11

                        This is a file manager application, this TreeView is the main file list view. Selection on click really looks horrible, and it's redundant - that's what the cursor is for (the thin dotted frame around an item). And not only that, but this selection upon click breaks other selection management operations.

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          Violet Giraffe
                          wrote on 4 Jun 2015, 06:26 last edited by
                          #12

                          So, there's no simple way to start dragging a specific item?

                          1 Reply Last reply
                          0

                          1/12

                          31 May 2015, 14:01

                          • Login

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