Results 1 to 8 of 8

Thread: Dropping files from the windows explorer into a subclassed QTreeWidget

  1. #1
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Dropping files from the windows explorer into a subclassed QTreeWidget

    Hi
    I try to implement to drop files from the windows explorer to a QTreeWidget (this one is subclassed clsMyTree (I need such feature to open the files and extract data to fill the tree).
    The problem that I have is that couldn't found any way to accept the QTreeWidget to see the drop object.
    See some test class below:
    Qt Code:
    1. ///////////////////start class //////////////////////////
    2. ///////////header file//////
    3. #include <QtGui/QApplication>
    4. #include <QtGui/QTreeWidget>
    5. class clsMyTree :
    6. public QTreeWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. clsMyTree(void);
    11. clsMyTree( QWidget *p=0); //: QTreeWidget(p) { }
    12. virtual ~clsMyTree(void);
    13. protected:
    14. virtual bool event(QEvent *event);
    15. virtual bool dropMimeData ( QTreeWidgetItem * parent, int index, const QMimeData * data, Qt::DropAction action );
    16. public slots:
    17. void setValue();
    18.  
    19. signals:
    20. void valueChanged(int newValue);
    21. };
    22.  
    23. ///////////cpp file//////
    24. #include "heaMyTree.h"
    25. #include <QDropEvent>
    26. clsMyTree::clsMyTree(void)
    27. {
    28. }
    29. clsMyTree::clsMyTree( QWidget *p) : QTreeWidget(p) {
    30. setAcceptDrops(true);
    31. p->setAcceptDrops(true);
    32. }
    33.  
    34. clsMyTree::~clsMyTree(void)
    35. {
    36. }
    37.  
    38. bool clsMyTree::dropMimeData ( QTreeWidgetItem * parent, int index, const QMimeData * data, Qt::DropAction action ){
    39. parent=parent;
    40. return true;
    41. }
    42.  
    43. bool clsMyTree::event(QEvent *event)
    44. {
    45. if (event->type() == QEvent::Drop) {
    46. QDropEvent *pEv=static_cast<QDropEvent *>(event);
    47. Qt::DropAction dropAct=pEv->dropAction ();
    48. long lActio=0;
    49. lActio=dropAct;
    50. }
    51.  
    52. return QWidget::event(event);
    53. }
    54.  
    55.  
    56. void clsMyTree::setValue()
    57. {
    58.  
    59. }
    60. ///////////////////end class //////////////////////////
    To copy to clipboard, switch view to plain text mode 
    With this example I catch one file from the windows explorer and try to drop it into the clsMyTree(inherited from QTreeWidget) and no event happen in my reimplemented functions ::event,dropMimeData and so on. Nor he display me the right mousecursor.
    For me it looks like the widget are not right enabled for dropping objects that are
    incomming out from another application. In this case windows explorer.
    I there somebody that have experience with such kind of works?
    Is the QTreeWidget the right widget for such things and is my implementation right or maybe I forgote something others?

    PS: The example was done with QT 4.1 and Visual Studio 2003. For the main
    widget I use also the designer. But unfortunately the new designer is for
    such works, especially for this drag and drop problem, not really
    helpfully.
    In the mean time I try to do this with KDevelop and KDE in Linux.
    I would really appreciate any helps.

    Thanks

    Piero
    Last edited by wysota; 14th April 2006 at 13:49. Reason: Missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dropping files from the windows explorer into a subclassed QTreeWidget

    I don't see the drop actually implemeneted anywhere in the code you have shown. Does the widget accept the drop or does it show the "stop sign"? You have to make sure it accepts appropriate mime-types (in case of files you'll get text/uri-list).

  3. #3
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dropping files from the windows explorer into a subclassed QTreeWidget

    Yes u have right. I could implement the drop (by examing the mime type and so on..) event, but the problem that have manifest earlier. As explaint before, when I start the drop action (by moving the mouse with the drag object over the QTreeWidget) in never function, espacially in the event function should be at least pass one of the overloaded functions ( I testet this in debug mode with break points in the following functions: event, dropEvent, dropMimeData). With this approach, I was also testing other drag & drops implentation with QT, and this was working.
    In my opinion the QTreeWidget is not ready to accept any drops, because the mouse cursor over the QTreeQidget does not change the Icon (shown that this object accept some drops).
    You have some another idea, or u see some other mess?
    I spent a lot of time and patience in it. I'm able to drag and drop object insight of the program, but I'm not able to communicate with drag and drop objects with QTreeWidget with other programms. And really I don't know where could be the diffrerence.

  4. #4
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dropping files from the windows explorer into a subclassed QTreeWidget

    Aha, I found aout that I have to implement de dragEnterEvent function.

    void clsMyTree::dragEnterEvent ( QDragEnterEvent * event ){
    if(event->mimeData()->hasUrls()==true){
    event->acceptProposedAction();
    QDrag *drag = new QDrag(this);
    QMimeData *mimeData = new QMimeData;
    QString qstrType;
    qstrType=event->format(0);

    mimeData->setData(qstrType , event->mimeData()->data(qstrType));
    drag->setMimeData(mimeData);

    Qt:ropAction dropAction = drag->start(Qt::CopyAction | Qt::MoveAction);
    event->acceptProposedAction();
    }
    }
    After this the dropMimeData function is called. But I'm not sure if this is the correct way.
    First it talkes a while until the function dropMimeData is called and second the mouse curosr resp. icon is still the blocked icon.
    Should I also changed the mouse icon ?
    This would be not so nice .

  5. #5
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dropping files from the windows explorer into a subclassed QTreeWidget

    You're doing it the bad way!!!

    Do NOT subclass events unless you have a revolutionnar D & D concept to implement! Everything is already done, all you need is to subclass :
    Qt Code:
    1. virtual bool dropMimeData( QTreeWidgetItem *parent, const QMimeData *data, Qt::DropAction action)
    To copy to clipboard, switch view to plain text mode 

    You may also want to read the docs to understand how to use it...
    Current Qt projects : QCodeEdit, RotiDeCode

  6. #6
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dropping files from the windows explorer into a subclassed QTreeWidget

    Sorry, but what you are talking is wrong!
    I did this exactly what you wrote before! In this way u could not receive any dropMimeData event if u want drag & Drop files from the windows explorer.
    I read also the docs, but nothing about such explanation.
    I donn't have problem with the drag & drops problem, but I have problem to drops files from outside of my program into to my QTreeWidget .
    Tell me more and not send me just one function what anyway my self already found in the documentation!

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Dropping files from the windows explorer into a subclassed QTreeWidget

    You really don't have to touch the event handlers here. All you need is already implemented by the abstract view class -- take a look at its sources, if you don't believe us, for example:

    Qt Code:
    1. void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. if (d_func()->canDecode(event)) {
    4. event->accept();
    5. setState(DraggingState);
    6. } else {
    7. event->ignore();
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    All things you need to reimplement are:
    bool QTreeWidget::dropMimeData ( QTreeWidgetItem * parent, int index, const QMimeData * data, Qt:ropAction action ) [virtual protected]
    Handles the data supplied by a drag and drop operation that ended with the given action in the index in the given parent item.
    See also supportedDropActions().

    QStringList QTreeWidget::mimeTypes () const [virtual protected]
    Returns a list of MIME types that can be used to describe a list of treewidget items.
    See also mimeData().

    QMimeData * QTreeWidget::mimeData ( const QList<QTreeWidgetItem *> items ) const [virtual protected]
    Returns an object that contains a serialized description of the specified items. The format used to describe the items is obtained from the mimeTypes() function.
    If the list of items is empty, 0 is returned rather than a serialized empty list.
    First method does the actual drop, second returns a list of mime-types which can be dropped onto the widget (maybe you forgot to implement it -- that's why I asked about that "stop sign" in my previous post) and the last one returns a pointer to an object describing a particular item according to mime-types returned by the previous method.

    You can take a look at equivalents of those three methods in the QAbstractItemModel class. And also remember that QTreeWidget is just a wrapper over QTreeView containing an internal model representation. For example:
    Qt Code:
    1. bool QTreeWidget::dropMimeData(QTreeWidgetItem *parent, int index,
    2. const QMimeData *data, Qt::DropAction action)
    3. {
    4. if (parent) idx = indexFromItem(parent);
    5. return model()->QAbstractItemModel::dropMimeData(data, action , index, 0, idx);
    6. }
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to wysota for this useful post:

    paltomare (15th April 2006)

  9. #8
    Join Date
    Apr 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Dropping files from the windows explorer into a subclassed QTreeWidget

    Yes, you got right !!!
    I had to implement the mimeTypes with the right mime type. This is what was missing.
    QStringList clsMyTree::mimeTypes () const{
    QStringList qstrList;
    //loading of mime types should be loaded over a ini files
    //here I hardcode this part
    qstrList.append("application/x-qabstractitemmodeldatalist");
    qstrList.append("text/html");
    qstrList.append("text/plain");
    qstrList.append("text/uri-list");
    qstrList.append("text/directory");
    qstrList.append("text/xml");
    return qstrList;
    }

    This is this part where I never understood.
    Thanks so much for your good explanation

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.