PDA

View Full Version : QListView



Yayati.Ekbote
27th February 2010, 07:09
Hi!!

While creating a simple file browser, i wrote the following code in the constructor of the class DirectoryView->

QFileSystemModel *model=new QFileSystemModel;
QListView *ListView=new QListView(this);
ListView->setGeometry(0,59,240,260);
model->setRootPath(QDir::rootPath());

ListView->setModel(model);
ListView->setRootIndex(model->index(QDir::rootPath()));
ListView->show();

ui->lineEdit->setText(QDir::rootPath());//to display the current path

Now if i want the ListView to show the items inside the folder item which is clicked....what should i do??

How can i get the path or QModelIndex of the item i click at runtime??
Plz guide..

Lykurg
27th February 2010, 10:09
See the signals QAbstractItemView::activated(), QAbstractItemView::clicked() ... and connect one of them to a custom slot, where you change the models root path according to the chosen folder.

EIDT: And use LAYOUTS! Do not use manual positioning for your widgets.

Yayati.Ekbote
1st March 2010, 11:41
But anyhow, i have to read the folder name when i click on any of the folder. For example in root directory, i have some folder, say "new folder". And my rootPath is "/root". And when i click on " new folder" my new rootPath must be "/root/new folder/. How it can be achieved by runtime random response of user? Plz guide a bit deep as i am unable to understand how to read the folder's name when user clicks on it. There's no method/property i've found to read the item's name. If listViewItem class is to be used then do i have to inherit the itemView class??.....
I tried the following code->
{
...
...
...
connect(listView,SIGNAL(clicked(QModelIndex)),this ,SLOT(setPath(QModelIndex)));
...
...
...
}

void DirectoryView::setPath(QModelIndex index)
{
QString basePath=QDir::rootPath();
QString currentPath=basePath+"\"+model->filePath(index);

listView->setRootPath(currentPath);
...
...
...
}

This code compiles fine but gives some runtime error when i click on any folder item!!!!
Please give me a deep guidance!!!!