Results 1 to 10 of 10

Thread: <SOLVED > Treeview select draged Item or set mime data from draged item

  1. #1
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default <SOLVED > Treeview select draged Item or set mime data from draged item

    Hi,

    I have a custom Treeview with a QFilemodel attached to it.
    The reason for making it custom is to be able to handel drag and drop myself.
    All I wanted to archiev is to get rid of the Pixmap that is taken from the draged Treeview Item.
    setPixmap does work if I set the mimeData myself.
    The Problem is to get the right Index for the mime Data.

    I tired:
    Qt Code:
    1. QModelIndexList tempindexlist;
    2. tempindexlist.append(selectedIndexes());
    3.  
    4. Drag.setMimeData(model()->mimeData(tempindexlist));
    To copy to clipboard, switch view to plain text mode 

    wich woks if I select the Item to be draged first. So I have to explicitly click on an Item first befor draging it.
    If I just drag it out of the treevie without clicking it previously, the indexlist contains the last selected index (or nothing if there is no selection).

    then I tried it this way:
    Qt Code:
    1. QModelIndexList tempindexlist;
    2. tempindexlist.append(currentIndex());
    To copy to clipboard, switch view to plain text mode 
    but this behaves exactly the same way.

    If I don't set the mimeData it works, but then the Pixmap is set automatically and i can't change it.
    Is there an equivalent of selectedIndexes for draged indexes?
    Any other way to solve this?
    Last edited by 0backbone0; 30th August 2015 at 16:07.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Treeview select draged Item or set mime data from draged item

    Quote Originally Posted by 0backbone0 View Post
    tired:
    Qt Code:
    1. QModelIndexList tempindexlist;
    2. tempindexlist.append(selectedIndexes());
    3.  
    4. Drag.setMimeData(model()->mimeData(tempindexlist));
    To copy to clipboard, switch view to plain text mode 

    wich woks if I select the Item to be draged first. So I have to explicitly click on an Item first befor draging it.
    If I just drag it out of the treevie without clicking it previously, the indexlist contains the last selected index (or nothing if there is no selection).
    Interesting, because the default implementation also does this:
    http://code.woboq.org/qt5/qtbase/src...10DropActionEE

    The selectedDraggableIndexes() are just the selectedIndexes() without those which are not draggable according to their item flags.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Treeview select draged Item or set mime data from draged item

    I got it working.
    The solution was to use IndexAt() with the event->pos() from mousePressEvent.
    Then I had to create a pointer to the attached model in order to use fileinfo on it.
    I then insert the resulting filename into an Urllist and set the mimeData with it.

    Qt Code:
    1. QFileSystemModel *tempmodel = qobject_cast<QFileSystemModel *>(model());
    2. QModelIndex tempindex = indexAt(event->pos());
    3. QMimeData *mimeData = new QMimeData;
    4. QList<QUrl>urllist;
    5. QString filename=tempmodel->filePath(tempindex);
    6. urllist.append(QString("file:"+filename));
    7. mimeData->setUrls(urllist);
    To copy to clipboard, switch view to plain text mode 

    Now setPixmap is working as expected and mimeData contains allways the currently dragged Item.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Treeview select draged Item or set mime data from draged item

    You should be able to get the filename from the model without casting, no?

    Regarding the difference in the default behavior and your workaround:
    are you trying to initiate the draw in a mouse event handler instead of in startDrag()?

    Cheers,
    _

  5. #5
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: <SOLVED > Treeview select draged Item or set mime data from draged item

    Yes, thats what I am doing right now;
    Qt Code:
    1. #include "customtreeview.h"
    2.  
    3. customtreeview::customtreeview(QWidget * parent)
    4. {
    5. }
    6.  
    7. void customtreeview::mousePressEvent(QMouseEvent *event)
    8. {
    9. if (event->button() == Qt::LeftButton)
    10. {
    11. QDrag Drag( this );
    12. QFileSystemModel *tempmodel = qobject_cast<QFileSystemModel *>(model());
    13. QModelIndex tempindex = indexAt(event->pos());
    14.  
    15. QPixmap tempmap(1,1);
    16. QMimeData *mimeData = new QMimeData;
    17. QList<QUrl>urllist;
    18. QString filename=tempmodel->filePath(tempindex);
    19.  
    20. emit setpreview(filename);
    21.  
    22. urllist.append(QString("file:"+filename));
    23. mimeData->setUrls(urllist);
    24. tempmap.fill(Qt::white);
    25. Drag.setPixmap(tempmap);
    26. Drag.setMimeData(mimeData);
    27. Drag.exec( Qt::IgnoreAction );
    28. }
    29. QTreeView::mousePressEvent(event);
    30. }
    To copy to clipboard, switch view to plain text mode 
    I left everything else untouched.

    I still struggle to understand how to set drag and drop behaviour correctly.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: <SOLVED > Treeview select draged Item or set mime data from draged item

    Is this a good idea?
    Won't that start a drag even when you are just clicking to select?

    My understanding is that the startDrag() method is where you should be starting drag operations, since it not only takes care of the mouse event handling, it also respects the "drag distance", i.e. the minimum mouse move while the button is down to start a drag.

    As a side note: that setUrls() call doesn't make much sense, you are not filling the list anywhere, right?

    Also, you can just use model()->data() or tempindex.data() to get the filename, no need for casting.

    Cheers,
    _

  7. #7
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: <SOLVED > Treeview select draged Item or set mime data from draged item

    Quote Originally Posted by anda_skoa View Post
    Is this a good idea?
    As a side note: that setUrls() call doesn't make much sense, you are not filling the list anywhere, right?
    I append the filename to it. Or is there a way to set just one Url directly?
    Qt Code:
    1. urllist.append(QString("file:"+filename));
    To copy to clipboard, switch view to plain text mode 

    How do you reimplement startDrag() ?

    I tried it this way?

    Qt Code:
    1. void customtreeview::startDrag()
    2. {
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 
    and put a qDebug in it, but it's never called.
    Otherwise what you say make perfect sense, but out of ignorance I am still stuck with my workaround.

    And model()->Data() just returns the filename without path. So it isn't much use to me either.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: <SOLVED > Treeview select draged Item or set mime data from draged item

    Quote Originally Posted by 0backbone0 View Post
    I append the filename to it. Or is there a way to set just one Url directly?
    Qt Code:
    1. urllist.append(QString("file:"+filename));
    To copy to clipboard, switch view to plain text mode 
    Right, sorry, missed that line.
    Better use QUrl::fromLocalFile() instead of string concatentation though.

    Quote Originally Posted by 0backbone0 View Post
    How do you reimplement startDrag() ?

    I tried it this way?

    Qt Code:
    1. void customtreeview::startDrag()
    To copy to clipboard, switch view to plain text mode 
    and put a qDebug in it, but it's never called.
    This is not the correct signature, you are missing the argument.


    Quote Originally Posted by 0backbone0 View Post
    And model()->Data() just returns the filename without path. So it isn't much use to me either.
    Ah, but you can also retrieve the path, QFileSystemModel::FilePathRole.
    Anyway, casting should be fine, but you might want to check the result of the cast for null pointer.

    Cheers,
    _

  9. #9
    Join Date
    Aug 2015
    Posts
    25
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: <SOLVED > Treeview select draged Item or set mime data from draged item

    The documentation sais:
    Qt Code:
    1. void QAbstractItemView::startDrag(Qt::DropActions supportedActions) [virtual protected]
    2. Starts a drag by calling drag->exec() using the given supportedActions.
    To copy to clipboard, switch view to plain text mode 
    If I use Qt:ropActions as an Argument I get the Error:

    variable or field 'startDrag' declared void
    void customtreeview::startDrag(Qt::IgnoreAction)

    What now? Any link to an example perhaps?

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: <SOLVED > Treeview select draged Item or set mime data from draged item

    Quote Originally Posted by 0backbone0 View Post
    The documentation sais:
    Qt Code:
    1. void QAbstractItemView::startDrag(Qt::DropActions supportedActions) [virtual protected]
    2. Starts a drag by calling drag->exec() using the given supportedActions.
    To copy to clipboard, switch view to plain text mode 
    Exactly!

    Quote Originally Posted by 0backbone0 View Post
    If I use Qt:ropActions as an Argument I get the Error:

    variable or field 'startDrag' declared void
    void customtreeview::startDrag(Qt::IgnoreAction)
    That looks wrong, almost like you had written exactly that instead of the actual method signature.

    Cheers,
    _

Similar Threads

  1. Finding a item in second column of treeView
    By cszawisza in forum Newbie
    Replies: 5
    Last Post: 20th September 2013, 01:19
  2. Auto Layout TreeView's item?
    By jerry7 in forum Qt Programming
    Replies: 3
    Last Post: 21st September 2010, 07:48
  3. Replies: 1
    Last Post: 28th July 2009, 07:58
  4. current item in treeview
    By kernel_panic in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2007, 20:26
  5. TreeView click item not connecting
    By Big Duck in forum Qt Programming
    Replies: 2
    Last Post: 9th June 2006, 20:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.