Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Questions about creating a mini file browser.
Forum Update on Monday, May 27th 2025

[SOLVED] Questions about creating a mini file browser.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leon
    wrote on 19 Jan 2014, 11:44 last edited by
    #1

    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..

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 19 Jan 2014, 17:08 last edited by
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on 19 Jan 2014, 19:10 last edited by
        #3

        [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

        1 Reply Last reply
        0

        1/3

        19 Jan 2014, 11:44

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved