Results 1 to 20 of 21

Thread: Drag and drop between model views

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Drag and drop between model views

    Quote Originally Posted by larry104
    Unfortunately, rewriting startDrag is a little bit of work. Thanks again for the help.
    Copy and paste it from Qt sources and add the lines you need.

  2. #2
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and drop between model views

    Hmmm, that might be embarrissing for me (after all I'm QT beginner) but I get compile errors when I just copy it.

    Qt Code:
    1. void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
    2. {
    3. QModelIndexList indexes = selectedIndexes();
    4. if (indexes.count() > 0) {
    5. // setup pixmap
    6. QRect rect = visualRect(indexes.at(0));
    7. QList<QRect> rects;
    8. for (int i = 0; i < indexes.count(); ++i) {
    9. rects.append(visualRect(indexes.at(i)));
    10. rect |= rects.at(i);
    11. }
    12. rect = rect.intersect(d->viewport->rect());
    13. QPixmap pixmap(rect.size());
    14. pixmap.fill(palette().base().color());
    15. QPainter painter(&pixmap);
    16. QStyleOptionViewItem option = viewOptions();
    17. option.state |= QStyle::State_Selected;
    18. for (int j = 0; j < indexes.count(); ++j) {
    19. option.rect = QRect(rects.at(j).topLeft() - rect.topLeft(),
    20. rects.at(j).size());
    21. itemDelegate()->paint(&painter, option, indexes.at(j));
    22. }
    23. painter.end();
    24. // create drag object
    25. QDrag *drag = new QDrag(this);
    26. drag->setPixmap(pixmap);
    27. drag->setMimeData(model()->mimeData(indexes));
    28. drag->setHotSpot(d->viewport->mapFromGlobal(QCursor::pos()) - rect.topLeft());
    29. if (drag->start(supportedActions) == Qt::MoveAction)
    30. d->removeSelectedRows();
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ../../../qt/qt-4.1.4/include/QtGui/qtableview.h: In member function `virtual
    2. void MyTableView::startDrag(QFlags<Qt::DropAction>)':
    3. ../../../qt/qt-4.1.4/include/QtGui/qtableview.h:134: `QTableViewPrivate*
    4. QTableView::d_func()' is private
    5. mytableview.cpp:43: within this context
    6. mytableview.cpp:43: cannot convert `QTableViewPrivate*' to `
    7. QAbstractItemViewPrivate* const' in initialization
    8. mytableview.cpp:53: invalid use of undefined type `struct
    9. QAbstractItemViewPrivate'
    10. ../../../qt/qt-4.1.4/include/QtGui/qabstractitemview.h:41: forward declaration
    11. of `struct QAbstractItemViewPrivate'
    12. mytableview.cpp:69: invalid use of undefined type `struct
    13. QAbstractItemViewPrivate'
    14. ../../../qt/qt-4.1.4/include/QtGui/qabstractitemview.h:41: forward declaration
    15. of `struct QAbstractItemViewPrivate'
    16. mytableview.cpp:71: invalid use of undefined type `struct
    17. QAbstractItemViewPrivate'
    18. ../../../qt/qt-4.1.4/include/QtGui/qabstractitemview.h:41: forward declaration
    19. of `struct QAbstractItemViewPrivate'
    20. make: *** [mytableview.o] Error 1
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Drag and drop between model views

    You can't use private components of Qt classes as they are not accessible, so you have to think a workaround for using Q_D and d->. It shouldn't be too hard as d->viewport should be available as viewport() and you can remove the selected rows without using the d->removeSelectedRows() call if you need that functionality. And when you won't be using "d" anymore, you can remove the Q_D() call.

  4. #4
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drag and drop between model views

    Works perfect now - thanks again so much for all the help.

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 outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 15:37
  3. Drag and drop revisited
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 30th June 2006, 16:41
  4. drag and drop with item views
    By castorvert in forum Qt Programming
    Replies: 14
    Last Post: 27th March 2006, 10:12
  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.