Results 1 to 2 of 2

Thread: some of QModel to QModel

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default some of QModel to QModel

    I made a model , a tree model to be precised and I want to copy an item in my tree including its children. What is the easiest and cleanest solution to this, I tried searching but wasn't able to find some samples. To the guru's please help the novice out

    baray98

  2. #2
    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: some of QModel to QModel

    Here's a small example which is capable of copying and pasting selected items. Unfortunately it doesn't take children into account, but that should be "only" a matter of reimplementing QAbstractItemModel::mimeData() and QAbstractItemModel::dropMimeData().
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class TreeView : public QTreeView
    4. {
    5. protected:
    6. void keyPressEvent(QKeyEvent *event)
    7. {
    8. if (event == QKeySequence::Copy)
    9. {
    10. QModelIndexList indexes = selectionModel()->selectedIndexes();
    11. QMimeData *data = model()->mimeData(indexes);
    12. QApplication::clipboard()->setMimeData(data);
    13. event->accept();
    14. }
    15. else if (event == QKeySequence::Paste)
    16. {
    17. QModelIndex index = currentIndex();
    18. const QMimeData *data = QApplication::clipboard()->mimeData();
    19. if (model()->dropMimeData(data, Qt::CopyAction, index.row() + 1, index.column(), index.parent()))
    20. setCurrentIndex(index.sibling(index.row() + 1, index.column()));
    21. event->accept();
    22. }
    23. else
    24. {
    25. QAbstractItemView::keyPressEvent(event);
    26. }
    27. }
    28. };
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QApplication app(argc, argv);
    33.  
    34. QStandardItem *parentItem = model.invisibleRootItem();
    35. for (int i = 0; i < 4; ++i) {
    36. QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
    37. parentItem->appendRow(item);
    38. parentItem = item;
    39. }
    40.  
    41. TreeView view;
    42. view.setModel(&model);
    43. view.show();
    44. return app.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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.