PDA

View Full Version : representation of path selected from filedialog in the tree form



merry
2nd February 2007, 09:19
Hi all

i am new in QT3.1 on Linux red hat
and i have a problem with QListView
i have a main window on that i have file menu futher having open (item)
when i select open then it show a file dialog from where i can select a path
now i want that what ever i select the path that path should come out in a tree structure(in hierarchy ) like first folder is root , subfolder,sub sub folder,filesetc



please do help me for this problem
thanks in advance

guilugi
2nd February 2007, 09:37
Well,

Is it not already the case ?
I don't remember well with Qt3, but it seems a file dialog returns you the absolute path...
Do you have an example ?

Guilugi.

merry
2nd February 2007, 10:11
thanks for reply
yes exactly file dialog returns the path....
and now i want that path should be viewed in a tree(hierarchy) on my mainwindow form
please do tell with an example
waiting for reply

wysota
2nd February 2007, 11:10
How about:

QListView lv;
QString path;
QStringList slist = QStringList::split("/", path);
QListViewItem *item = 0;
while(!slist.empty()){
QString elem = slist.front();
slist.pop_front();
if(!item){
item = new QListViewItem(&lv);
} else {
item = new QListViewItem(item);
}
item->setText(elem);
}

merry
6th February 2007, 05:56
Want to Thank u Lots and Lots of time ...

The code u sent is working .. .. .....


Once again Thanx..

merry
6th February 2007, 13:51
In this it displays the selected path in the form of tree but i want to display all the files and folders of the last item of the selected path that is last item of the selected path is root and it displays all the files and folders contained in the root item.


Foreg.

the selected path is /home/qt/etc

here in the selected path "etc" should be the root item and it displays all the files and folders contained in the "etc" in the the form of the tree structure.

Please reply me as soon as possible its really very very urgent

Thanx

wysota
6th February 2007, 13:56
In this it displays the selected path in the form of tree but i want to display all the files and folders of the last item of the selected path that is last item of the selected path is root and it displays all the files and folders contained in the root item.


And the problem is...?

QDir::entryList() or QDir::entryInfoList() will give you a list of entries in a particular directory. Just wrap the result into QListViewItems and you're done.


Please reply me as soon as possible its really very very urgent
Please don't say that, it won't make anyone answer your post faster than without it...

merry
6th February 2007, 14:47
Thanx 4 the reply

But it be better if You explain it with me the help of an example or code.

wysota
6th February 2007, 14:51
Oh come on, what is there to explain?


QStringList slist = QDir("/etc/").entryList();
for(QStringListIterator it = slist.begin(); it!=slist.end(); ++it){
new QListViewItem(*it, listview);
}

merry
7th February 2007, 10:03
Hi

:crying: I am sending u the code Please tell me its right or not for the representation of path in a tree structure form.
actually I got " segmentationfault "

Here the code is


#include <qfiledialog.h>
#include <qapplication.h>
#include <qlistview.h>
#include <qstring.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qstringlist.h>
#include<qmessagebox.h>
#include <qpixmap.h>
#include <qcheckbox.h>
#include <qdir.h>
#include <qstringlist.h>
#include <qmessagebox.h>

void Form1::fileOpen()
{

m_source = QFileDialog::getExistingDirectory(
"/",
this,
"get existing directory",
"Choose a directory",
TRUE );
setDir(m_source);
}

void Form1::setDir(QString path)
{

QListViewItemIterator it(listView);
++it;
for(;it.current();++it)
{
it.current()->setOpen(FALSE);
}


QStringList slist;
QDir dir(path);
QListViewItem * item;
item->firstChild();
slist=QStringList::split("/",path);
slist=dir.entryList();
QStringList::Iterator it2=slist.begin();
for(;it2!=slist.end();++it2)
{
while(item)
{
if(item->text(0)=*it2)
{
item->setOpen(TRUE);
break;
}
item=item->itemBelow();

}

}

if ( item )
listView->setCurrentItem( item );


}

If there is some alterations in this code then also tell me.

Thanx

wysota
7th February 2007, 10:26
Why don't you debug the code and see where it segfaults? This is only a snippet, so I can't debug it for you. Please use a debugger and print a backtrace to see where and why it crashes.

merry
7th February 2007, 10:38
Thanx 4 ur reply:) and I also dont want YOU to debug for me.

Once again Thanx

jacek
7th February 2007, 14:30
Make sure you initialize all of the variables you use in Form1::setDir().