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 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drag & Drop using QTableWidget

    Bump. ghorwin 'hijacked' my thread!

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

    Question Re: Drag & Drop using QTableWidget

    Israa: Sorry for hijacking, but you asked excactly the same question that I had, so figured opening a new thread would be non-sense.

    Jpn: dragMoveEvent() is called correctly.

    So it seems something prevents the dropping...

    If I read the documentation correctly, only the widget itself, that should accept the drop, needs to call setAcceptDrop(), right? Or do I also have to tell the parent dialog to accept drops?

    Anyway, I'm clueless. Should I post a simple test-program?
    Andreas

  3. #3
    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
    Jpn: dragMoveEvent() is called correctly.
    Yes, but do you accept the event?
    J-P Nurmi

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

    Thumbs up Re: Drag & Drop using QTableWidget

    Guys, got it to work!!!

    The trick is to call
    Qt Code:
    1. setDragDropMode(QAbstractItemView::DropOnly);
    To copy to clipboard, switch view to plain text mode 
    in addition to
    Qt Code:
    1. setAcceptDrops(true);
    To copy to clipboard, switch view to plain text mode 
    (wasn't mentioned in the docu, but I just went back to the abstract view class)...

    Now dropEvent is called alright!
    Andreas

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

    Default Re: Drag & Drop using QTableWidget

    Still something strange about the dropping...

    Explain that: When I reimplement the dragMoveEvent() (even with an empty function), I can drop anywhere in my table. If I don't reimplement it, I can only drop when the cursor is exactly on the frame around the content rows (so at the bottom of the header row and at the top of the horizontal scroll bar). That's weird, if you ask me.

    So, the recipe for dropping on QTableWidget is:

    1. setup your table in the constructor
    Qt Code:
    1. setDragDropMode(QAbstractItemView::DropOnly);
    2. setAcceptDrops(true);
    To copy to clipboard, switch view to plain text mode 

    2. re-implement the functions dragEnterEvent(...), dragMoveEvent(...) and dropEvent(...), for example:
    Qt Code:
    1. void MyTable::dragEnterEvent(QDragEnterEvent *event)
    2. {
    3. if (event->mimeData()->hasUrls()) {
    4. event->acceptProposedAction();
    5. }
    6. }
    7.  
    8. void MyTable::dragMoveEvent ( QDragMoveEvent * ) {
    9. // nothing to do, function just has to exist...
    10. }
    11.  
    12. void MyTable::dropEvent(QDropEvent *event)
    13. {
    14. if (event->mimeData()->hasUrls()) {
    15. QList<QUrl> urls = event->mimeData()->urls();
    16. for (int i=0; i<urls.count(); ++i) {
    17. int row = rowCount();
    18. insertRow(row);
    19. QTableWidgetItem *newItem = new QTableWidgetItem(urls[i].toString());
    20. setItem(row, 0, newItem);
    21. }
    22.  
    23. event->accept();
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 
    At least that's what worked for me.
    Andreas

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

    Default Re: Drag & Drop using QTableWidget

    I still haven't gotten an answer to my question, would anyone know what I need to do to enable drag & drop to and from 2 QTableWidgets? I create a class that inherits QTableWidget and I place the two objects on either side of a QSplitter. I can drag & drop in the same table, but not to the other table.

  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

    Using Drag and Drop with Item Views - Using Convenience Views:
    For example, we can enable drag and drop in a list widget with the following lines of code:

    Qt Code:
    1. QListWidget *listWidget = new QListWidget(this);
    2. listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    3. listWidget->setDragEnabled(true);
    4. listWidget->setAcceptDrops(true);
    5. listWidget->setDropIndicatorShown(true);
    To copy to clipboard, switch view to plain text mode 
    The same applies for a table widget.
    J-P Nurmi

  8. #8
    Join Date
    Oct 2006
    Posts
    5
    Qt products
    Qt4

    Default Re: Drag & Drop using QTableWidget

    Quote Originally Posted by Israa View Post
    I still haven't gotten an answer to my question, would anyone know what I need to do to enable drag & drop to and from 2 QTableWidgets? I create a class that inherits QTableWidget and I place the two objects on either side of a QSplitter. I can drag & drop in the same table, but not to the other table.
    I am having the same problem, trying to drag&drop from one table to another.

    Quote Originally Posted by jpn View Post
    I am having trouble following the page you linked. I have tried the function calls that enable drag&drop functionality, but I am still having issues dropping to a different table than was dragged from. Is there any way some short example code could be provided? Nothing complicated, I'd just like to see a working example.

    Thanks.

  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

    Quote Originally Posted by para View Post
    I am having the same problem, trying to drag&drop from one table to another.
    Does this work for you?
    Qt Code:
    1. #include <QtGui>
    2.  
    3. QTableWidget* createTableWidget(QWidget* parent)
    4. {
    5. QTableWidget* tableWidget = new QTableWidget(4, 4, parent);
    6. tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    7. tableWidget->setDragEnabled(true);
    8. tableWidget->setAcceptDrops(true);
    9. tableWidget->setDropIndicatorShown(true);
    10. return tableWidget;
    11. }
    12.  
    13. int main(int argc, char* argv[])
    14. {
    15. QApplication a(argc, argv);
    16. QSplitter splitter;
    17. splitter.addWidget(createTableWidget(&splitter));
    18. splitter.addWidget(createTableWidget(&splitter));
    19. splitter.show();
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. #10
    Join Date
    Oct 2006
    Posts
    5
    Qt products
    Qt4

    Default Re: Drag & Drop using QTableWidget

    Quote Originally Posted by jpn View Post
    Does this work for you?
    Yeah that worked, thanks a lot. I must have missed something.


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.