Results 1 to 5 of 5

Thread: QDirModel and QTreeView cut and paste

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2007
    Posts
    56
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: QDirModel and QTreeView cut and paste

    Quote Originally Posted by lyuts View Post
    What if you call refresh( const QModelIndex & ) for the parents of files that have been "cut&pasted"? and then repaint for QTreeView?
    I haven't subclassed QTreeView so I don't have access to the repaint. I'm trying to use the standard widgets if possible.

  2. #2
    Join Date
    Jun 2007
    Posts
    56
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    6
    Thanked 1 Time in 1 Post

    Talking Re: QDirModel and QTreeView cut and paste

    I have a solution using an event filter shown below.

    One would think that one would be able to intercept an event of type QEvent;;Drop heading for the TreeViews but I was unable to detect one. Strange...Unless it is the MetaCall event it receives just after the drop. Since I could not make use of the information in the meta call event I can't know if it was the Drop event or not.

    At anyrate, the QEvent::ChildRemoved event type proved to work just as well. Perhaps even better since I should only get that when the model/view accepts the drop and I don't have to put in lots of ugly code to send the event and check its return to see if the drop was accepted or not.

    Enjoy...

    -Mic

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MainWindow( QWidget *parent = 0 ) : QMainWindow( parent)
    9. {
    10. splitter = new QSplitter( this );
    11.  
    12. model = new QDirModel( this );
    13. model->setReadOnly( false );
    14.  
    15. tree = new QTreeView;
    16. tree->setModel( model );
    17. tree->setRootIndex( model->index( QString( "/home/randy" ) ) );
    18.  
    19. model1 = new QDirModel( this );
    20. model1->setReadOnly( false );
    21.  
    22. righttree = new QTreeView;
    23. righttree->setModel( model1 );
    24. righttree->setRootIndex( model1->index( QString( "/tmp" ) ) );
    25.  
    26. tree->setDragEnabled( true );
    27. tree->setAcceptDrops( true );
    28. tree->setDropIndicatorShown( true );
    29. tree->setDragDropOverwriteMode( false );
    30. tree->installEventFilter( this );
    31.  
    32. righttree->setDragEnabled( true );
    33. righttree->setAcceptDrops( true );
    34. righttree->setDropIndicatorShown( true );
    35. righttree->setDragDropOverwriteMode( false );
    36. righttree->installEventFilter( this );
    37.  
    38. splitter->addWidget( tree );
    39. splitter->addWidget( righttree );
    40.  
    41. setCentralWidget( splitter );
    42. }
    43.  
    44. bool eventFilter( QObject *target, QEvent *event )
    45. {
    46. if( target == tree && event->type() == QEvent::ChildRemoved ) {
    47. model->refresh( tree->rootIndex() );
    48. }
    49. else if( target == righttree && event->type() == QEvent::ChildRemoved ) {
    50. model1->refresh( righttree->rootIndex() );
    51. }
    52. return false;
    53. }
    54.  
    55. private:
    56. QSplitter *splitter;
    57. QDirModel *model;
    58. QTreeView *tree;
    59. QDirModel *model1;
    60. QTreeView *righttree;
    61. QTimer *timer;
    62. };
    63.  
    64. int main( int argc, char *argv[] )
    65. {
    66. // a "file manager" at simplest :)
    67. QApplication a( argc, argv );
    68. MainWindow window;
    69. window.show();
    70. return a.exec( );
    71. }
    72.  
    73. #include "app.moc"
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QDirModel and QTreeView cut and paste

    I admit I haven't read the whole thread, but based on the parts I did read I can say that you have to make sure you return Qt::MoveAction as the drop action (cut and paste is really another interface to drag&drop). Depending on how you handle the D&D mechanism (meaning if you do it in the model or in the view), there are different ways you can do it. Otherwise you'll have to detect the move action yourself and delete entries from the model as you probably did using the event filter.

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.