Results 1 to 4 of 4

Thread: QMimeData problems between a QTableWidget and a QTreeWidget

  1. #1
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QMimeData problems between a QTableWidget and a QTreeWidget

    Hello, i am trying to make it so when you drag from a table view to a tree view you can send some data and add an item to the tree view. I searched on the web and programmed something that i feel should work but, alas, it is not.
    This is the treeview class.

    Qt Code:
    1. Favorites::Favorites(QWidget *parent)
    2. : QTreeWidget(parent)
    3. {
    4. //building the part for the favorites list
    5. QTreeWidgetItem* favoritesItem = new QTreeWidgetItem();
    6. favoritesItem->setText(0,"Favorites");
    7. this->addTopLevelItem(favoritesItem);
    8. favoritesItem->setExpanded(true);
    9. this->setDragDropMode(QAbstractItemView::DropOnly);
    10. this->setAcceptDrops(true);
    11. addPathToFavorites(QString("asdsadasd"));
    12. refreshFavorites();
    13. }
    14.  
    15. void Favorites::dragEnterEvent(QDragEnterEvent* event){
    16. qDebug()<< event->mimeData()->text(); // just prints out ""
    17. if (event->mimeData()->hasText())
    18. event->acceptProposedAction();
    19. }
    20.  
    21. void Favorites::dropEvent(QDropEvent *event){
    22. qDebug()<< event->mimeData()->text();
    23. addPathToFavorites(event->mimeData()->text());
    24. }
    To copy to clipboard, switch view to plain text mode 

    And this is the table

    Qt Code:
    1. TableWidget::TableWidget(QWidget *parent)
    2. : QTableWidget(parent)
    3. {
    4. //i've enabled drag in the main class
    5. }
    6.  
    7. void TableWidget::mousePressEvent(QMouseEvent *event){
    8. if(event->button() == Qt::LeftButton)
    9. poss = event->pos();
    10. else { ;}
    11. QTableWidget::mousePressEvent(event);
    12. }
    13.  
    14. void TableWidget::mouseMoveEvent(QMouseEvent *event){
    15. if(!event->buttons() & Qt::LeftButton)
    16. QTableWidget::mouseMoveEvent(event);
    17. else
    18. if((event->pos() - poss).manhattanLength() < QApplication::startDragDistance())
    19. QTableWidget::mouseMoveEvent(event);
    20. else{
    21. QTableWidgetItem *item = this->itemAt(event->pos());
    22. if(item){
    23. QDrag* drag = new QDrag(this);
    24. QMimeData* mimeData = new QMimeData;
    25. mimeData->setText(item->text());
    26. drag->setMimeData(mimeData);
    27. qDebug()<<drag->mimeData()->hasText()<<drag->mimeData()->text();//print's what it should
    28. }
    29. QTableWidget::mouseMoveEvent(event);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? Any help will be greatly appreciated. Thank you.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData problems between a QTableWidget and a QTreeWidget

    Hi!

    No time to look into it thoroughly. Aren't you missing drag->exec() ?

    What happens when you drag some text from another source/application? That way you can separate source and destination problems..

    If nothing works - Post a minimal compilable example, please!

    HIH

    Johannes

  3. #3
    Join Date
    Jul 2009
    Posts
    12
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData problems between a QTableWidget and a QTreeWidget

    I am a bit shocked I missed that. It works now.
    But I have a follow-up question: just by using drag->exec(), it works, but when I am over the place I want to drop the data, it shows a blocked-like icon(i can't take a screenshot of it); any ideas why this is happening? should i pass something in the exec method call ?
    Thanks again for the help.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMimeData problems between a QTableWidget and a QTreeWidget

    Thats a destination problem - should also happen, when you try tro drag text from let's say notepad..

    You need to accept the drag in the dragMoveEvent, too.

    Qt Code:
    1. void Favorites::dragMoveEvent(QDragMoveEvent* event)
    2. {
    3. if (event->mimeData()->hasText())
    4. {
    5. event->accept();
    6. }
    7. QTreeWidget::dragMoveEvent(event);
    8. }
    To copy to clipboard, switch view to plain text mode 

    And call the inherited dragEnterEvent, also!

    HIH

    Johannes

  5. The following user says thank you to JohannesMunk for this useful post:

    andreime (23rd March 2010)

Similar Threads

  1. Drag&Drop - Pointer into QMimeData?
    By droetker in forum Newbie
    Replies: 2
    Last Post: 11th October 2009, 14:14
  2. QTableWidget problems
    By eleanor in forum Qt Programming
    Replies: 6
    Last Post: 22nd May 2009, 13:18
  3. Replies: 2
    Last Post: 17th March 2008, 12:53
  4. Is there a known problem with QMimeData on Mac OS X?
    By Wurgl in forum Qt Programming
    Replies: 8
    Last Post: 27th February 2008, 22:21
  5. MIME database and QMimedata
    By nupul in forum Qt Programming
    Replies: 12
    Last Post: 12th April 2006, 14:36

Tags for this Thread

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.