Results 1 to 9 of 9

Thread: Drag an item from QTreeWidget and drop in TableView

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Drag an item from QTreeWidget and drop in TableView

    Hi,

    I have a treewidget of which I want to be able to drag items from and drop them in a tableview, the tableview uses a model so after the drop I would need to update the data structures my model uses. Is it possible to drag an item from treewidget to tableview?

    Regards,
    Steve

  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: Drag an item from QTreeWidget and drop in TableView

    Yes. Make sure they are aware of each others mime types (or use the same).

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag an item from QTreeWidget and drop in TableView

    Thanks,

    They will be using the same data ( the data is shared from another class ), the data is only text so I'm sure the mime type will be fine. Guess I start with using QDrag and capture mousePressEvent?

    Regards,
    Steve

  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: Drag an item from QTreeWidget and drop in TableView

    The data is irrelevant. You have to program models (or items) so that they use the same MIME.

  5. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag an item from QTreeWidget and drop in TableView

    Ok, guess I need to do some reading on QMimeData...

    Also, would I need to have my QTableView and QTreeWidget derived in my own classes in order to capture the mouse events, I've added a mouseEvent to my QTreeWidget form and nothing happens, never gets called...

    Regards,
    Steve

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag an item from QTreeWidget and drop in TableView

    It is not mouseEvent but there are three mouse event handlers:
    mouseMoveEvent, mouseReleaseEvent and mousePressEvent.
    You should reimplement these, and it will work.

  7. #7
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag an item from QTreeWidget and drop in TableView

    Ok,

    I was just reading the documentation on Drag and Drop in the QT assistant and it shows an example using mousePressEvent which is what I was meant to say! I have this event in my tree widget, but it never gets called?

    Code :

    Qt Code:
    1. DAMessageTree::DAMessageTree(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ui.setupUi(this);
    5. list << "Messages";
    6. ui.treeList->setHeaderLabels( list );
    7.  
    8. QTreeWidgetItem* pRoot = new QTreeWidgetItem((QTreeWidget*)0,QStringList(QString("Can Messages")));
    9. ui.treeList->addTopLevelItem( pRoot );
    10. ui.treeList->expandItem( pRoot );
    11. QList<QTreeWidgetItem *> items;
    12.  
    13. if( theApp->m_dcb.m_oMessages.count() > 0 )
    14. {
    15. QHashIterator<QString, QObject*> i( theApp->m_dcb.m_oMessages );
    16. while( i.hasNext() )
    17. {
    18. i.next();
    19. CDADcb::CMessage* pMess = (CDADcb::CMessage*)i.value();
    20. if( pMess )
    21. {
    22. QTreeWidgetItem *newItem = new QTreeWidgetItem( pRoot );
    23. newItem->setFlags( newItem->flags() | Qt::ItemIsUserCheckable );
    24. //newItem->setCheckState(Qt::Unchecked);
    25. newItem->setText( 0, pMess->name );
    26. // store object with item for later retreivel
    27. newItem->setData( 0, Qt::UserRole, QVariant::fromValue(pMess) );
    28. ui.treeList->addTopLevelItem( newItem );
    29. ui.treeList->expandItem( newItem );
    30. for( int i = 0; i < pMess->m_oSignals.count(); i++ )
    31. {
    32. CDADcb::CSignal* pSig = (CDADcb::CSignal*)pMess->m_oSignals.at(i);
    33. if( pSig )
    34. {
    35. QTreeWidgetItem *newSigItem = new QTreeWidgetItem( newItem );
    36. newSigItem->setText( 0, QString( pSig->name ) );
    37. ui.treeList->addTopLevelItem( newSigItem );
    38. }
    39. }
    40.  
    41. }
    42. }
    43. }
    44.  
    45. }
    46.  
    47.  
    48. DAMessageTree::~DAMessageTree()
    49. {
    50.  
    51. }
    52.  
    53.  
    54. void DAMessageTree::mousePressEvent ( QMouseEvent * event )
    55. {
    56. }
    To copy to clipboard, switch view to plain text mode 

    Something I'm doing wrong

    Thanks,
    Steve

  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: Drag an item from QTreeWidget and drop in TableView

    IMO you don't have to reimplement mouse handlers. Simply reimplement mimeData(), dropMimeData() and mimeTypes() in the tree widget and in the model that handles the table and make sure they are able to handle each other's data.

  9. #9
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drag an item from QTreeWidget and drop in TableView

    I'm thinking now of implementing dragEnterEvent, dragMoveEvent and dropEvent...As for mousePressEvent, this would be useful for getting the mouse position at the start of the drag for later use - I believe if the distance between the start mouse position and the position where the mouse button was pressed is larger than QApplication's recommended drag start distance we can avoid initiating a drag just because say the user's hand shakes...

    Regards,
    Steve

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.