Micawber
28th May 2008, 15:18
I'm using the examples of the simple file manager using QDirModel and TreeView mentioned in this (http://www.qtcentre.org/forum/f-qt-programming-2/t-suggestionsideas-about-a-file-browser-page2-1625.html) post.
#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow( QWidget *parent = 0 ) : QMainWindow( parent)
{
splitter = new QSplitter( this );
model = new QDirModel( this );
model->setReadOnly( false );
tree = new QTreeView;
tree->setModel( model );
tree->setRootIndex( model->index( QString( "/home/randy" ) ) );
model1 = new QDirModel( this );
model1->setReadOnly( false );
righttree = new QTreeView;
righttree->setModel( model1 );
righttree->setRootIndex( model1->index( QString( "/tmp" ) ) );
tree->setDragEnabled( true );
tree->setAcceptDrops( true );
tree->setDropIndicatorShown( true );
tree->setDragDropOverwriteMode( false );
righttree->setDragEnabled( true );
righttree->setAcceptDrops( true );
righttree->setDropIndicatorShown( true );
righttree->setDragDropOverwriteMode( false );
splitter->addWidget( tree );
splitter->addWidget( righttree );
timer = new QTimer( this );
//QObject::connect( model, SIGNAL( layoutChanged( ) ), this, SLOT( refreshleft( ) ) );
//QObject::connect( model1, SIGNAL( layoutChanged( ) ), this, SLOT( refreshright( ) ) );
QObject::connect( timer, SIGNAL( timeout( ) ), this, SLOT( refreshleft( ) ) );
QObject::connect( timer, SIGNAL( timeout( ) ), this, SLOT( refreshright( ) ) );
setCentralWidget( splitter );
timer->start( 1000 );
}
private slots:
void refreshleft ()
{
model->refresh( tree->rootIndex() );
}
void refreshright ()
{
model1->refresh( righttree->rootIndex() );
}
private:
QSplitter *splitter;
QDirModel *model;
QTreeView *tree;
QDirModel *model1;
QTreeView *righttree;
QTimer *timer;
};
int main( int argc, char *argv[] )
{
// a "file manager" at simplest :)
QApplication a( argc, argv );
MainWindow window;
window.show();
return a.exec( );
}
#include "app.moc"
I am running into a problem when I do cut and paste in that when using the shift key modifier to indicate a true cut and paste (instead of a copy and paste), I see the new file has been inserted in the tree that received the drop, however the tree that initiated the drag still shows the file. I checked in the file system and the file has in fact been removed so the cut and paste worked, its just that since QDirModel uses a cache, it needs a refresh.
Any ideas on how to force a QDirModel->refresh() upon successful cut and paste? (I would have thought the thing would have done it automatically.)
I have a workaround by starting a 1 second timer that calls refresh on timeout but thats not exactly what I want.
I tried connecting QDirModel->dataChanged to a slot that calls refresh on the QDirModel but that didn't work because that doesn't get emitted when new data is added.
I then tried connecting QDirModel->layoutChanged to that slot but that caused the app to lock up.
#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow( QWidget *parent = 0 ) : QMainWindow( parent)
{
splitter = new QSplitter( this );
model = new QDirModel( this );
model->setReadOnly( false );
tree = new QTreeView;
tree->setModel( model );
tree->setRootIndex( model->index( QString( "/home/randy" ) ) );
model1 = new QDirModel( this );
model1->setReadOnly( false );
righttree = new QTreeView;
righttree->setModel( model1 );
righttree->setRootIndex( model1->index( QString( "/tmp" ) ) );
tree->setDragEnabled( true );
tree->setAcceptDrops( true );
tree->setDropIndicatorShown( true );
tree->setDragDropOverwriteMode( false );
righttree->setDragEnabled( true );
righttree->setAcceptDrops( true );
righttree->setDropIndicatorShown( true );
righttree->setDragDropOverwriteMode( false );
splitter->addWidget( tree );
splitter->addWidget( righttree );
timer = new QTimer( this );
//QObject::connect( model, SIGNAL( layoutChanged( ) ), this, SLOT( refreshleft( ) ) );
//QObject::connect( model1, SIGNAL( layoutChanged( ) ), this, SLOT( refreshright( ) ) );
QObject::connect( timer, SIGNAL( timeout( ) ), this, SLOT( refreshleft( ) ) );
QObject::connect( timer, SIGNAL( timeout( ) ), this, SLOT( refreshright( ) ) );
setCentralWidget( splitter );
timer->start( 1000 );
}
private slots:
void refreshleft ()
{
model->refresh( tree->rootIndex() );
}
void refreshright ()
{
model1->refresh( righttree->rootIndex() );
}
private:
QSplitter *splitter;
QDirModel *model;
QTreeView *tree;
QDirModel *model1;
QTreeView *righttree;
QTimer *timer;
};
int main( int argc, char *argv[] )
{
// a "file manager" at simplest :)
QApplication a( argc, argv );
MainWindow window;
window.show();
return a.exec( );
}
#include "app.moc"
I am running into a problem when I do cut and paste in that when using the shift key modifier to indicate a true cut and paste (instead of a copy and paste), I see the new file has been inserted in the tree that received the drop, however the tree that initiated the drag still shows the file. I checked in the file system and the file has in fact been removed so the cut and paste worked, its just that since QDirModel uses a cache, it needs a refresh.
Any ideas on how to force a QDirModel->refresh() upon successful cut and paste? (I would have thought the thing would have done it automatically.)
I have a workaround by starting a 1 second timer that calls refresh on timeout but thats not exactly what I want.
I tried connecting QDirModel->dataChanged to a slot that calls refresh on the QDirModel but that didn't work because that doesn't get emitted when new data is added.
I then tried connecting QDirModel->layoutChanged to that slot but that caused the app to lock up.