Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: QDropEvent

  1. #1
    Join Date
    May 2009
    Posts
    129

    Default QDropEvent

    Hi All

    i am facing the same QTreeWidget Drag and Drop issue since last 1.5 weeks..

    I am feeling this is very bad me..

    My problem is How can i set top level items only has to accepts drops.

    I set following things

    ui->widget->dragDropOverwriteMode();
    ui->treeWidget->setDragEnabled(true);
    ui->treeWidget->setAcceptDrops(true);
    ui->treeWidget->setDragDropMode(QAbstractItemView ::InternalMove);


    I know answer is very simple, But i don't know how do compare the Drop Event with top level items ...

    please help me to sort out this issue..

    Any suggestions welcome.

    Thanks

    Addu R

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

    Default Re: QDropEvent

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    Dear wysota

    Thanks for your reply..

    I enabled the Drop action only for top level items by

    QTreeWidgetItem * friends = new QTreewidgetItem(ui->treeWidget);

    freinds->setFlags(Qt::Items is Drop Enabled);

    but same thing all items are accepting the drops...

    And one more thing dafter dropping the any one item to friends ,it behaving like child item


    Thanks

    Addu R

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

    Default Re: QDropEvent

    Make sure InternalMove is disabled. If that doesn't help, please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    Previously i used the internalmove mode, Then i changed to DragDrop mode.. But it is not working for me ..

    sample code

    Qt Code:
    1. ui->treeWidget->dragDropOverwriteMode();
    2. ui->treeWidget->setDragEnabled(true);
    3. ui->treeWidget->setAcceptDrops(true);
    4. ui->treeWidget->setDragDropMode(QAbstractItemView ::DragDrop);
    5. QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget);
    6. item->setText(0,"Friends");
    7. QList <QString > list;
    8. list << tr("john") << tr("joseph") << tr("stephen") << tr("raj") << tr("Addu") ;
    9.  
    10. for ( int i=0;i < list.size;i++)
    11. {
    12. QTreeWidgetItem *new_item = new QTreeWidgetItem(item);
    13. new_item->setText(0,list.at(i));
    14. }
    To copy to clipboard, switch view to plain text mode 

    Again i am creating new top level item for TreeWidget

    Qt Code:
    1. QTreeWidgetItem *colleages = new QTreeWidgetItem(ui->treewidget);
    2. colleages->setText(0,"co-Workers");
    To copy to clipboard, switch view to plain text mode 

    I am trying to drag the items from friends to co-workers .. Here top level item only accept the drops..child items won't accept the drops.


    How do i sort out this issue


    Thanks

    Addu R

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

    Default Re: QDropEvent

    Ok, but how do you set the flags and how do you handle the drop itself?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    I set the flags only for top level items ..

    Qt Code:
    1. item->setFlag(Qt::ItemisDropEnabled);
    2. colleages->setFlag(Qt::ItemisDropEnabled);
    3. new_item->setFlag(Qt::Itemis DragEnabled);
    To copy to clipboard, switch view to plain text mode 

    Previoused i over ride the DropEvent function..
    But if i set the ui->treeWidget->setAcceptDrops(true),DropEvent function was not called ..

    So I commented the DropEvent function..

    And i did like this

    Qt Code:
    1. void MainWindow::dropEvent(QDropEvent *event)
    2. {
    3. ui->treeWidget->setDropEvent(event) // Here Application is crashing
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    Where i have done mistake,please correct me, i know problem is at DropEvent function..

    please help me to clear this issue

    Thanks

    Addu R

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

    Default Re: QDropEvent

    The mistake you did was that you didn't bother to look into the documentation.

    QTreeWidget::dropMimeData()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    Dear Wysota..

    Thanks for your valuable solution.

    I implemented the following function
    Qt Code:
    1. void MainWindow::dragEnterEvent(QDragEnterEvent *eve)
    2. {
    3. qDebug() << "Drag enter event";
    4. eve->accept();
    5.  
    6. }
    7. void MainWindow::dragMoveEvent(QDragMoveEvent *e)
    8. {
    9.  
    10. qDebug() << "Drag move event";
    11. e->accept();
    12. }
    13. bool MainWindow::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
    14. {
    15. QMessageBox::information(0,"",QString("Addu"));
    16. }
    To copy to clipboard, switch view to plain text mode 

    but complier is not going to dropMimeData function

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

    Default Re: QDropEvent

    Because you still didn't bother to look into the docs. This time I will not give you a pointer but instead you're given a classic RTFM. I'll give you a hint though - you want to read about performing drag and drop with Item Views.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    Wysota

    i have gone through the below link

    http://www.qtsoftware.com/developer/...-23.4700087965..

    same thing only i followed ...please i request you provide me one sample example


    Thanks

    Addu R

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

    Default Re: QDropEvent

    Learn, don't copy without thinking.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    I am trying to execute sample example.. while executing i am getting following errors

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QTreeWidgetItem>
    6. #include <QMimeData>
    7.  
    8. namespace Ui
    9. {
    10. class MainWindowClass;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20. bool dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action);
    21. QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const;
    22. private:
    23. Ui::MainWindowClass *ui;
    24. protected:
    25. void dragEnterEvent(QDragEnterEvent e);
    26. void dragMoveEvent(QDragMoveEvent e) ;
    27.  
    28.  
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    6. {
    7. ui->setupUi(this);
    8. ui->treeWidget->setColumnCount(2);
    9. ui->treeWidget->setAcceptDrops( true );
    10. // The tree supports dragging of its own items
    11. ui->treeWidget->setDragEnabled(true);
    12. QTreeWidgetItem *itemOne = new QTreeWidgetItem(ui->treeWidget);
    13. QTreeWidgetItem *itemTwo = new QTreeWidgetItem(ui->treeWidget);
    14. QTreeWidgetItem *itemThree = new QTreeWidgetItem(ui->treeWidget);
    15.  
    16. itemOne->setText(0, "John");
    17. itemTwo->setText(0, "Peter");
    18. itemThree->setText(0, "Kumar");
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    25. QMimeData *MainWindow::mimeData(const QList<QTreeWidgetItem *> items) const
    26. {
    27. // Create a QByteArray to store the data for the drag.
    28. QByteArray text;
    29. // Create a data stream that operates on the binary data
    30. QDataStream ds(&text, QIODevice::WriteOnly);
    31. // Add each item's text for col 0 to the stream
    32. for (int i=0;i<items.size();i++)
    33. ds << items.at(i)->text(0);
    34. QMimeData *md = new QMimeData;
    35. // Set the data associated with the mime type foo/bar to ba
    36. md->setData("foo/bar", text);
    37. return md;
    38. }
    39. bool MainWindow::dropMimeData(QTreeWidgetItem *parent, int index, const
    40. QMimeData *data, Qt::DropAction action)
    41. {
    42. if (parent) {
    43. // Create a QByteArray from the mimedata associated with foo/bar
    44. QByteArray text = data->data("foo/bar");
    45. // Create a data stream that operates on the binary data
    46. QDataStream ds(&text, QIODevice::ReadOnly);
    47. while (!ds.atEnd()) {
    48. QString str;
    49. // Read a byte from the stream into the string
    50. ds >> str;
    51. // Create a new item that has the item that is dropped on as a parent
    52.  
    53. QTreeWidgetItem *newItem = new QTreeWidgetItem(parent);
    54. newItem->setText(0, str);
    55. }
    56. }
    57. return true;
    58. }
    59. void MainWindow::dragEnterEvent(QDragEnterEvent event)
    60. {
    61. event.accept();
    62. }
    63.  
    64.  
    65. void MainWindow::dragMoveEvent(QDragMoveEvent event)
    66. {
    67. event.accept();
    68. }
    To copy to clipboard, switch view to plain text mode 

    errors:
    error: `event' has incomplete type
    error: forward declaration of `struct QDragEnterEvent'
    error: invalid use of undefined type `struct QDragEnterEvent'
    error: forward declaration of `struct QDragEnterEvent'
    error: `event' has incomplete type

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

    Default Re: QDropEvent

    You didn't include proper files so the compiler complains about it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    Hi wysota

    In above example .. when running the application mimedata, Dropmimedata is never invoked..

    I put the QmessageBox and qDebug() statement there.. But i didn't get any responce from that functions..


    Thanks

    Yuvaraj R

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

    Default Re: QDropEvent

    It will never be invoked because you're overriding the whole drag&drop mechanism present in Item Views. And you still didn't bother to read the docs...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    Hi wysota..

    I have been reading the doc and right now i am referring the item/view based Puzzle example...

    whenever make drag the data are stored at time mime data function will be called ..

    whenver we dropping the item that time dropmime data function will be called..

    if i don't override drag and drop functions also that two functions are not invoking...



    Thanks

    Addu R

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

    Default Re: QDropEvent

    You only need to override mimeData() and dropMimeData(), nothing more.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    May 2009
    Posts
    129

    Default Re: QDropEvent

    Dear Wysota

    I over ride the drag and drop function by setting the QTreeWidget class as a parent..

    sample code

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include <QTreeWidgetItem>
    4. #include <QtGui/QMainWindow>
    5. #include <QDropEvent>
    6. namespace Ui
    7. {
    8. class MainWindowClass;
    9. }
    10.  
    11. class MainWindow : public QMainWindow,public QTreeWidget
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18. bool dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action);
    19. QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const;
    20. void dragEnterEvent(QDragEnterEvent *e);
    21. void dragMoveEvent(QDragMoveEvent *e) ;
    22. private:
    23. Ui::MainWindowClass *ui;
    24.  
    25. };
    26.  
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMessageBox>
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent),QTreeWidget(parent), ui(new Ui::MainWindowClass)
    6. {
    7. ui->setupUi(this);
    8. ui->treeWidget->setColumnCount(1);
    9. ui->treeWidget->setAcceptDrops( true );
    10. // The tree supports dragging of its own items
    11. ui->treeWidget->setDragEnabled(true);
    12. QTreeWidgetItem *itemOne = new QTreeWidgetItem(ui->treeWidget);
    13. QTreeWidgetItem *itemTwo = new QTreeWidgetItem(ui->treeWidget);
    14. QTreeWidgetItem *itemThree = new QTreeWidgetItem(ui->treeWidget);
    15. itemOne->setText(0, "John");
    16. itemTwo->setText(0, "Peter");
    17. itemThree->setText(0, "Kumar");
    18. }
    19. QMimeData *MainWindow::mimeData(const QList<QTreeWidgetItem *> items) const
    20. {
    21. QMessageBox::information(0,"",QString("Drag and Drop"));
    22. // Create a QByteArray to store the data for the drag.
    23. QByteArray text;
    24. // Create a data stream that operates on the binary data
    25. QDataStream ds(&text, QIODevice::WriteOnly);
    26. // Add each item's text for col 0 to the stream
    27. for (int i=0;i<items.size();i++)
    28. ds << items.at(i)->text(0);
    29. QMimeData *md = new QMimeData;
    30. // Set the data associated with the mime type foo/bar to ba
    31. md->setData("foo/bar", text);
    32. return md;
    33. }
    34. bool MainWindow::dropMimeData(QTreeWidgetItem *parent, int index, const
    35. QMimeData *data, Qt::DropAction action)
    36. {
    37.  
    38. if (parent) {
    39. // Create a QByteArray from the mimedata associated with foo/bar
    40. QByteArray text = data->data("foo/bar");
    41. // Create a data stream that operates on the binary data
    42. QDataStream ds(&text, QIODevice::ReadOnly);
    43. while (!ds.atEnd()) {
    44. QString str;
    45. // Read a byte from the stream into the string
    46. ds >> str;
    47. // Create a new item that has the item that is dropped on as a parent
    48.  
    49. QTreeWidgetItem *newItem = new QTreeWidgetItem(parent);
    50. newItem->setText(0, str);
    51. }
    52. }
    53. return true;
    54. }
    55. void MainWindow::dragEnterEvent(QDragEnterEvent *e)
    56. {
    57. e->accept();
    58. }
    59.  
    60.  
    61. void MainWindow::dragMoveEvent(QDragMoveEvent *e)
    62. {
    63. e->accept();
    64. }
    65.  
    66. MainWindow::~MainWindow()
    67. {
    68. delete ui;
    69. }
    To copy to clipboard, switch view to plain text mode 


    I have one doubt .,Mainwindow class having two sub classes.which sub class drag enter & drag move functions it will take..

    please clear me

    Thanks

    Addu R

  20. #20
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDropEvent

    hey man.. u cannot inherit from both QMainWindow and QTreeWidget. make seperate classes for both of them.. ..
    tomoro if i get the Fkin time i will read your posts and problems..

Similar Threads

  1. May I get XEvent from QDropEvent?
    By alfa_wu in forum Qt Programming
    Replies: 4
    Last Post: 19th March 2007, 01:43

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.