PDA

View Full Version : How to display Details of Audio File in QTreeView



owais_blore
22nd November 2012, 11:25
I need to display details of all .mp3 files present in my drives. I am using QTreeView to display. Here is the code:

// Displays Files in Detail View on Clicking Drive
void DetailView::on_DriveView_clicked(const QModelIndex &index)
{
int m_count=0;
QStandardItemModel *model = new QStandardItemModel(0,0);
QString sPath = m_SystemModel->fileInfo(index).absoluteFilePath();
m_SystemListViewModel->setRootPath(sPath);
ui->DriveListView->setRootIndex(m_SystemListViewModel->index(sPath));

m_SystemModel->setRootPath(QDir::currentPath());
m_SystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs );
m_SystemListViewModel->setFilter( QDir::Files | QDir::NoDotAndDotDot );

QStringList m_list;
QDirIterator dirIt(sPath,QDirIterator::Subdirectories);

while (dirIt.hasNext())
{
dirIt.next();
if (QFileInfo(dirIt.filePath()).isFile())
{
if (QFileInfo(dirIt.filePath()).suffix() == "mp3" || QFileInfo(dirIt.filePath()).suffix() == ".avi")
{
m_list << dirIt.filePath();

QModelIndex index = model->index(m_count, 0, QModelIndex());
model->setHeaderData( 0, Qt::Horizontal, "File Name" );
model->setHeaderData( 1, Qt::Horizontal, "Size" );
model->setHeaderData( 2, Qt::Horizontal, "Type" );
model->setHeaderData( 3, Qt::Horizontal, "Date Modified" );

model->setData( index, dirIt.fileName(), Qt::DecorationRole );
QStandardItem *item = new QStandardItem(dirIt.fileName());
model->setItem(m_count, item);

ui->DriveListView->setModel(model);
m_count++;
}
}
m_SystemListViewModel->setNameFilterDisables(false);
}
}
Have a look at my code, even though I have used setHeaderData for Size, Type and Date Modified, it gets displayed when run the application but when i click any drive, only File Name appears and rest doesn't get displayed. I am using QTreeView, as of now my code displays all the .mp3 files under Name category. I want to display the Size, Date Modified, Type etc details too. Here is sample pic:
8452
How to achieve it?

anda_skoa
23rd November 2012, 14:52
You only set data for the first column, if you want data in the other columns you have to add that data to the model.

The view can only display data that the model provides. Your model only knows about the file name, it can't provide any other data yet.

Cheers,
_

owais_blore
26th November 2012, 06:30
Thanks buddy. As explained, I tried adding data to model and its working. Here is the code:


void DetailView::on_DriveView_clicked(const QModelIndex &index)
{
int m_count_row = 0;

QStandardItemModel *model = new QStandardItemModel(0,0);
QString sPath = m_SystemModel->fileInfo(index).absoluteFilePath();
m_SystemListViewModel->setRootPath(sPath);
ui->DriveListView->setRootIndex(m_SystemListViewModel->index(sPath));

m_SystemModel->setRootPath(QDir::currentPath());
m_SystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs );
m_SystemListViewModel->setFilter( QDir::Files | QDir::NoDotAndDotDot );

QStringList m_list;
QDirIterator dirIt(sPath,QDirIterator::Subdirectories);

while (dirIt.hasNext())
{
dirIt.next();
if (QFileInfo(dirIt.filePath()).isFile())
{
if (QFileInfo(dirIt.filePath()).suffix() == "mp3" ||(QFileInfo(dirIt.filePath()).suffix() == "mts" ) ||(QFileInfo(dirIt.filePath()).suffix() == "m2ts" ))
{
m_list << dirIt.filePath();

QModelIndex m_index = model->index(m_count_row, 0, QModelIndex());
model->setHeaderData( 0, Qt::Horizontal, "Name" );
model->setHeaderData( 1, Qt::Horizontal, "Type" );
model->setHeaderData( 2, Qt::Horizontal, "Size" );
model->setHeaderData( 3, Qt::Horizontal, "Date Modified" );

model->setData( m_index, dirIt.fileName(), Qt::DecorationRole );
QStandardItem *itemName = new QStandardItem(dirIt.fileName());
model->setItem(m_count_row, 0, itemName);

model->setData( m_index, dirIt.fileInfo().suffix(), Qt::DecorationRole );
QStandardItem *itemExtention = new QStandardItem( dirIt.fileInfo().suffix());
model->setItem(m_count_row, 1, itemExtention);

model->setData( m_index, dirIt.fileInfo().size(), Qt::DecorationRole );
float fFileSize = dirIt.fileInfo().size();
float fFileKB = fFileSize / 1024; //kilobyte
float fFileMB = fFileKB / 1024; //megabyte
float fFinalSize = ceilf(fFileMB * 100) / 100;

QString sSizeValue = QString::number(fFinalSize);
QStandardItem *itemSize = new QStandardItem(sSizeValue + " MB");
model->setItem(m_count_row, 2, itemSize);

ui->DriveListView->setModel(model);
ui->DriveListView->setRootIsDecorated(false);
m_count_row++;
}
}

m_SystemListViewModel->setNameFilterDisables(false);
}
}


You only set data for the first column, if you want data in the other columns you have to add that data to the model.

The view can only display data that the model provides. Your model only knows about the file name, it can't provide any other data yet.

Cheers,
_