PDA

View Full Version : Adding QStringList to QListView



Tomasz
30th September 2010, 16:13
Hello!

I've listed some directories to QStringList. Now I want to put that list into QListView. How can I do it? My code:



void MainWindow::listMyDirs()
{
QDir myDir("/home/");
QStringList list = myDir.entryList (QDir::Dirs|QDir::NoDotAndDotDot, QDir::Name);

// ui->listaCzujnikow->setModel() or smething??
}


thanks in advance
best regards
Tomasz

Lykurg
30th September 2010, 16:23
See QStringListModel.

EDIT: Ehm, better use QFileSystemModel direct!

Tomasz
30th September 2010, 21:10
See QStringListModel.

How can I use it here? Some code would be very helpful.

I've tried:



QDir myDir("/home/");

QStringListModel list(myDir.entryList (QDir::Dirs|QDir::NoDotAndDotDot, QDir::Name));

ui->myList->setModel(&list);


but it doesn't work. It's compiling but list is empty...

thanks in advance
best regards
Tomasz

Lykurg
30th September 2010, 21:29
Old same c++ weakness... Create it on the heap.

Tomasz
30th September 2010, 21:33
It works! Thanks! But why it's necessary to create that object on heap to make it work?

thanks in advance
best regards
Tomasz

Lykurg
30th September 2010, 21:57
Because otherwise your model gets deleted at the scope's end. So when your view is displayed the model does not exist any more. And without a model, there is nothing to display.