[SOLVED] Questions about creating a mini file browser.



  • What i want to achieve is a mini file browser like this
    !http://i.imgur.com/7ZyOGq6.png(1)!

    which shows only folders and .mp3 files
    So until now i have achieved someting like these:
    !http://i.imgur.com/XY5eKTG.png(2)!

    With this code:
    @void MainWindow::on_files_path_textChanged(const QString &path)
    {
    if(path.isEmpty() || !QDir(path).exists())
    return;

    //we want the path to always end with a "/". Also we want to see if the user
    //just deleted a trailing "/", if so do not take actions.
    if(path.right(1)!="\\" && path.right(1)!="/")
    {
        if(currentPath.left(currentPath.count()-1)==path)
            return;
        currentPath=QDir::toNativeSeparators(path+"/");
    }
    else
    {
        if(currentPath==path)
            return;
        currentPath=path;
    }
    
    updateFilesListWidget();
    
    //monitor the path for any possible change at it's subfolders/audio files
    if(watchFolders_)
        delete watchFolders_;
    watchFolders_ = new QFileSystemWatcher(this);
    connect(watchFolders_, SIGNAL(directoryChanged(QString)), this, SLOT(updateFilesListWidget()));
    
    if(currentPath.count()!=3)
        settings.setValue("current_path", currentPath.left(currentPath.count()-1));
    else //for hard drives we want to save them as c:/ not as c:
        settings.setValue("current_path", currentPath.left(currentPath.count()));
    

    }

    void MainWindow::updateFilesListWidget()
    {
    QDir dir(ui->files_path->text());

    ui->files_listWidget->clear();
    
    QStringList list_of_subfolders = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
    for(int i=0; i<list_of_subfolders.count(); i++)
    {
        QListWidgetItem *folder = new QListWidgetItem;
        folder->setText(list_of_subfolders.at(i));
        folder->setIcon(QIcon::fromTheme("folder", QIcon(":/icons/Pictures/folder_icon.png")));
        ui->files_listWidget->addItem(folder);
    }
    
    QStringList list_of_audio_files = dir.entryList(QStringList("*.mp3"), QDir::Files ,QDir::Name);
    for(int i=0; i<list_of_audio_files.count(); i++)
    {
        QListWidgetItem *audio_file = new QListWidgetItem;
        audio_file->setText(list_of_audio_files.at(i));
        audio_file->setIcon(QIcon::fromTheme("audio-x-generic", QIcon(":/icons/Pictures/audio_icon.png")));
        ui->files_listWidget->addItem(audio_file);
    }
    

    }

    void MainWindow::on_files_listWidget_itemDoubleClicked(QListWidgetItem *item)
    {
    ui->files_path->setText(currentPath+item->text());
    }@

    My problem here, is that i am using a folder/audio icon of mine, and not using system's default and generally this seems a bad way to do a file browser...

    i tried with
    @QFileSystemModel model = new QFileSystemModel;
    model->setNameFilters(QStringList("
    .mp3"));
    model->setNameFilterDisables(false);
    model->setRootPath(path);
    ui->treeView->setModel(model);
    ui->treeView->setRootIndex(model->index(path));@

    which creates this
    !http://i.imgur.com/HGU87io.png(3)!

    but i don't know how to hide the Size/Type/Date Modified and also the triangle to show the subfolders of this folder..



  • Just quick thought. How about using listView to show the FileSystemModel ?
    @
    QListView *lView = new QListView;
    lView->setModel(model);@



  • [quote author="Dheerendra" date="1390151334"]Just quick thought. How about using listView to show the FileSystemModel ?
    @
    QListView *lView = new QListView;
    lView->setModel(model);@[/quote]

    Yep it works :)
    Thank you, didn't thought about it


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.