PDA

View Full Version : How get the files names and put them into listWidget



Alan_K
3rd April 2011, 18:26
Hi all.

I have quite a large problem, I have no idea how to get the names of all files in the directory "data" and insert in to listWidget (addItem?). My code looks like this:


void printDialog::getFiles()
{
QDir dir("data");
if (!dir.exists())
{
qDebug() << "Komunikat: niepoprawna sciezka (data)";
QDir().mkdir("data");
QMessageBox::information(this, "Błąd", "Nie odnaleziono katalogu <i>data</i>, przez co program nie mógł dalej działać. Folder został utworzony automatycznie, aby kontynuować użyj tej funkcji jeszcze raz.");
}
else
{
// yyy... :<
}
}

I don't have any idea - nothing I have achieved.

stampede
3rd April 2011, 18:42
I assume you want us to fill the "yyyy... :<" part :P
Have you read QDir docs, for example there is entryList (http://doc.qt.nokia.com/latest/qdir.html#entryList-2) method, set a proper filter as a parameter (http://doc.qt.nokia.com/latest/qdir.html#Filter-enum) and you have first part of the issue solved.
For the second part, you can use addItem (http://doc.qt.nokia.com/latest/qlistwidget.html#addItem) for each item in list created in previous step.

Alan_K
3rd April 2011, 19:35
Yeah, I did it! Thanks. :cool:



QStringList filters;
filters << "*.html";
dir.setNameFilters(filters);
ui->listWidget->clear();
ui->listWidget->addItems(dir.entryList(QDir::NoDotAndDotDot | QDir::Files));