Results 1 to 20 of 22

Thread: Drag & Drop using QTableWidget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag & Drop using QTableWidget

    For drag and drop, take a look at Using Drag and Drop with Item Views - Using Convenience Views.

    For context menu, you might want to reimplement contextMenuEvent() instead. More or less something like this:
    Qt Code:
    1. void table_widget::contextMenuEvent(QContextMenuEvent *event) {
    2. QMenu popupMenu(this);
    3. popupMenu.addAction(...);
    4. popupMenu.exec(event->globalPos());
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  2. #2
    Join Date
    Feb 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag & Drop using QTableWidget

    I fixed that problem, I just had to create a new QTableWidgetItem for each cell in the table.
    I did have another question though, for dropEvent, it doesnt seem like the function is being called. I have it set up so that it prints a message when it enters that function but the message is never displayed. What could be wrong?
    Here's the code for that function:

    Qt Code:
    1. void table_widget::dropEvent(QDropEvent *event)
    2. {
    3. printf("dropEvent\n");
    4. event->acceptProposedAction();
    5. }
    To copy to clipboard, switch view to plain text mode 

    I even tried inserting if (event->mimeData()->hasText()) before the printf() statement but that doesnt really do anything.

  3. #3
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drag & Drop using QTableWidget

    Hello,

    You have to do those two steps :

    1 - call setAcceptDrops(true) on your tablewidget
    2 - reimplement dragEnterEvent method, and accept it !

    I'm not sure if the second step is absolutely necessary for a tablewidget, but the first is, for sure

    Guilugi

  4. #4
    Join Date
    Feb 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag & Drop using QTableWidget

    I just had to reimplement dragMoveEvent() and it worked fine, thanks.

    Now I'm having another 'problem'. In my project I'm trying to drag and drop from one QTableWidget to another but I can't quite get it right. The dragEnterEvent acknowledges when I've entered another QTableWidget but thats about it. Here's what I'm working with:

    Qt Code:
    1. void table_widget::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. QTableWidgetItem* test = itemAt(event->pos());
    4. if (test != 0)
    5. {
    6. if ( event->source() == this ) // same table, move entry
    7. {
    8. event->setDropAction( Qt::MoveAction );
    9. event->accept();
    10. }
    11. else // different table, add entry
    12. {
    13. printf("Different table\n");
    14. event->acceptProposedAction();
    15. }
    16.  
    17. source = itemAt(event->pos());
    18. source->setText(itemAt(event->pos())->text());
    19. }
    20. }
    21.  
    22. void table_widget::dropEvent(QDropEvent *event)
    23. {
    24. printf("I'm in dropEvent\n");
    25. itemAt(event->pos())->setText(source->text());
    26. if(source != itemAt(event->pos()))
    27. source->setText("");
    28. event->acceptProposedAction();
    29. }
    30.  
    31. void table_widget::dragMoveEvent(QDragMoveEvent *event)
    32. {
    33. event->setDropAction(Qt::MoveAction);
    34. event->accept();
    35. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions?
    Last edited by Israa; 3rd March 2007 at 20:30.

  5. #5
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drag & Drop using QTableWidget

    I have exactly the same problem. I called set setAcceptDrops(true) and reimplemented dragEnterEvent(QDragEnterEvent *event), which gets called and I accept the proposed copy-action.

    However, my reimplemented dropEvent function is never called. The documentation and examples say it will be called, but I couldn't get this to work :-(

    Btw, this is Qt Open Source 4.2.2.

    Any suggestions?

  6. #6
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Exclamation Re: Drag & Drop using QTableWidget

    Correction to my last post: It is Qt Open Source 4.2.1

    I believe I have found the problem. In qtablewidget.h, in the class declaration for QTableWidget the function dropEvent() is not declared as virtual, thus our reimplemented dropEvent() function will never be called.

    I guess I have to check with 4.2.2 and see if this was fixed...

    Andreas

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag & Drop using QTableWidget

    Quote Originally Posted by ghorwin View Post
    I believe I have found the problem. In qtablewidget.h, in the class declaration for QTableWidget the function dropEvent() is not declared as virtual, thus our reimplemented dropEvent() function will never be called.
    It's declared virtual in QWidget.
    http://www.qtcentre.org/forum/p-reim...ostcount5.html
    J-P Nurmi

  8. #8
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    108
    Thanks
    9
    Thanked 12 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Drag & Drop using QTableWidget

    Yes, that's why it works when you inherit from QTableView... however, QTableWidget provides its own implementation of dropEvent() without declaring it virtual. Therefore classes deriving from QTableWidget will not be able to reimplement that function, or if they do the function will never get called.

    Andreas

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Drag & Drop using QTableWidget

    Actually, it doesn't matter if QTableWidget marks it virtual or not. It has already been declared as virtual in the base class, in QWidget that is.
    Quote Originally Posted by jacek View Post
    Once a method was declared as virtual, it will be virtual in all subclasses, regardless if one uses the "virtual" keyword or not.
    J-P Nurmi

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 20:36
  2. Drag and Drop
    By allensr in forum Qt Programming
    Replies: 1
    Last Post: 11th December 2006, 20:50
  3. Replies: 7
    Last Post: 8th September 2006, 16:19
  4. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41
  5. Drag 'n Drop problem
    By kiker99 in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2006, 16:35

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
  •  
Qt is a trademark of The Qt Company.