PDA

View Full Version : How to display entire directory(Filesystem)



deepakn
6th November 2009, 05:54
Hi all
I have attached a picture, please check that.
It shows the 'Light Explorer' plugin for Notepad++. Basically, it lists down all the contents of the file system on the right hand pane and one can double click on a file to open it in notepad++.

Could someone tell me how can I bring in that full directory tree? Any kind of help will be highly appreciated.

What we are intending to create is a file previewer - Directory structure on left side and a basic text viewer on right side. So whenever a file is clicked on the left side, the text viewer should immediately show the contents of the file.

Edit: This app is to be deployed in Fedora 7 machine even though i have taken the windows file structure as an example.

Thanks a lot in advance :)
Deepak

yogeshgokul
6th November 2009, 06:14
For directory structure data you need to use QDirModel. This model will give you data for underlign file structure. And you can use QTreeView to display that in the left pane of your desired application.
On right habd pane you need to use QTextBrowser/QTextEdit/QSimpleTextBrowser.
See this example:
http://doc.trolltech.com/4.5/itemviews-dirview.html

squidge
6th November 2009, 07:56
I would have thought a QFileSystemModel would be better, coupled with a QTreeView. Its threaded, so doesn't handle the ui thread, and detects changes to the file system, applying them dynamically.

JD2000
6th November 2009, 09:27
Hi,

Deja Vu?

You may be interested in my earlier post:

"QT Designer 4.5" on 3rd November 2009 at 14:03

You can of course substitute the QFileSystemModel in place of QDirModel as suggested by fatjuicymole.

deepakn
10th November 2009, 06:25
Hi all,
Thanks a lot to everyone for the solutions. It really helped. That example alone was too helpful.

I had been through JD2000's post. I too follow the same method of designing the UI and then programming the functionality into it.

Now I am stuck with another (dumb)problem.
I have this QDirModel inside a QTreeView as intended. Now I need to create a signal-slot relationship between 'clicked' on the treeView and a slot which does some manipulations on the file which is selected. My problem is that I am not able to return the filePath from the QDirModel/QTreeView as a string.
I could find that
QString QDirModel::filePath ( const QModelIndex & index ) const does it, but I am not able to supply the index value of the currently selected item. How do I do it?

This maybe a dumb question and I am sorry for it.
Thanks for all the help

Deepak

yogeshgokul
10th November 2009, 06:42
QString QDirModel::filePath ( const QModelIndex & index ) const [/CODE] does it, but I am not able to supply the index value of the currently selected item. How do I do it?
Lets say you want to do something when someone double clicks on any file in tree view.
You need to connect the
void doubleClicked ( const QModelIndex & index ) SIGNAL of the QTreeView.
To your custom SLOT.
In that slot, you can get the filePath easily because you have the QModelIndex with you. :)
By calling

QString QDirModel::fileName ( const QModelIndex & index ) const

deepakn
10th November 2009, 07:07
Thanks a lot Yogesh. Now I have this question related to Model-View architecture( I am totally new to it. )

How do I access the 'filePath' of this QDirModel from another function?


fileLoader::fileLoader( QWidget *parent ) : QDialog( parent )
{
ui.setupUi( this );

//QFileSystemModel *model = new QFileSystemModel;
QDirModel *model = new QDirModel;
ui.tree->setModel(model);

connect(ui.tree, SIGNAL( doubleClicked( const QModelIndex & )), this, SLOT(previewFile( const QModelIndex & )));

}

void fileLoader::previewFile( const QModelIndex & index )
{
//QString filePath = ui.tree ->filePath( index );
}

I know that
QString filePath = ui.tree ->filePath( index ); is wrong. But how do I access that particular function? What is the right procedure to do it?

yogeshgokul
10th November 2009, 07:17
There is a significant problem with your code.

QDirModel *model = new QDirModel; Should be a class level variable.


I know that is wrong. But how do I access that particular function? What is the right procedure to do it?


void fileLoader::previewFile( const QModelIndex & index )
{
QString strFileName = model->fileName(index);
QFile f(strFileName);
if(f.open(QIOdevice::ReadOnly))
{
QString data = f.readAll();
f.close();
yourTextWidget->setText(data);
}
}

ashukla
10th November 2009, 08:56
Hi all
I have attached a picture, please check that.
It shows the 'Light Explorer' plugin for Notepad++. Basically, it lists down all the contents of the file system on the right hand pane and one can double click on a file to open it in notepad++.

Could someone tell me how can I bring in that full directory tree? Any kind of help will be highly appreciated.

What we are intending to create is a file previewer - Directory structure on left side and a basic text viewer on right side. So whenever a file is clicked on the left side, the text viewer should immediately show the contents of the file.

Edit: This app is to be deployed in Fedora 7 machine even though i have taken the windows file structure as an example.

Thanks a lot in advance :)
Deepak
You can also list all the directories, subdirectries, files (hidden also) using
ls -Ra
command and QProcess object.

yogeshgokul
10th November 2009, 09:21
You can also list all the directories, subdirectries, files (hidden also) using
ls -Ra
command and QProcess object.
And how many years it will take to display that structure in a tree view :D

squidge
10th November 2009, 09:56
You can also list all the directories, subdirectries, files (hidden also) using
ls -Ra
command and QProcess object.

Not exactly portable though is it?

squidge
10th November 2009, 09:58
How do I access the 'filePath' of this QDirModel from another function?As yogeshgokul says is one way of doing it, and the cleanest. Or you could use the modal() method of the treeview, cast it to a QDirModel and use that. It all depends on the access you have to appropriate variables.