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] Changing rootpath of directory model in combo box.
QtWS25 Last Chance

[Solved] Changing rootpath of directory model in combo box.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 5.4k 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.
  • T Offline
    T Offline
    ThaRez
    wrote on last edited by
    #1

    Hello
    I'm trying to achieve a file manager that includes a combo box that lists the files in the currently selected folder.
    My software consists of two windows (+ the combo box) where one displays the folders and the second the files in the currently
    selected folder. This files list is what I'd like to duplicate to the combo box. I now have two QFileSystemModels, one that shows the folders and a second for displaying the files. I've attached these to the appropriate tree view & list view. Besides this, I've also attached the file model to my combo box. I've then added a function which updates the rootpath of the file model whenever the folder is changed:

    @
    void MainWindow::on_treeView_clicked(const QModelIndex &index)
    {
    QString sPath = dirmodel->fileInfo(index).absoluteFilePath();
    ui->listView->setRootIndex(filemodel->setRootPath(sPath));
    ui->comboBox->setRootIndex(filemodel->setRootPath(sPath)); // FAILS!
    }
    @

    Unfortunately I'm unable to update the root index as it's not provided by the combo box.
    The root set is the beginning is though correctly shown in the combo box, I just can't manage
    to get it to change.

    I'd appreciate if someone could guide me how to properly do this task. Thank you!
    Best regards
    Richard

    Edit: Please use @ tags around code sections so it becomes easier to read; Andre

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      This basically works for me:

      @
      class MainWindow : public QMainWindow
      {
      Q_OBJECT

      public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();

      protected slots:
      void setComboRootIndex(const QModelIndex &index);

      private:
      Ui::MainWindow *ui;
      QFileSystemModel *_fsm;
      };

      MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
      {
      ui->setupUi(this);

      // the ui has a QTreeView in ui->treeView
      // and a QComboBox in ui->comboBox
      
      // initialize the file system model
      _fsm = new QFileSystemModel(this);
      _fsm->setRootPath("");
      
      // set the model to the views
      ui->treeView->setModel(_fsm);
      ui->comboBox->setModel(_fsm);
      
      // optional
      //QModelIndex startIndex = _fsm->index(QDir::homePath());
      //ui->treeView->setRootIndex(startIndex);
      //ui->comboBox->setRootModelIndex(startIndex);
      
      connect(ui->treeView, SIGNAL(clicked(QModelIndex)),
              this, SLOT(setComboRootIndex(QModelIndex)));
      

      }

      void MainWindow::setComboRootIndex(const QModelIndex &index)
      {
      qDebug() << "index " << index << "=" << _fsm->filePath(index);
      if(_fsm->canFetchMore(index))
      // make sure the entries in the dir are loaded
      _fsm->fetchMore(index);
      ui->comboBox->setRootModelIndex(index);
      }
      @

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • T Offline
        T Offline
        ThaRez
        wrote on last edited by
        #3

        thank you for your reply, I'll try it tomorrow and confirm if it worked for me, changing the topic to solved in that case.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          ThaRez
          wrote on last edited by
          #4

          ok, I managed to get the code tried out. It almost works :) That is, it does update the drop down when I press the drop down arrow, but the "one line visible, selected item" does not change when I change the folder location. Now if I switch folder it still shows a file from the last folder as selected until I press the drop down, updating the list. Any suggestions how to manage this? I greatly appreciate the help!
          Best regards
          Richard

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            You can add

            @
            ui->comboBox->setCurrentIndex(0);
            @

            at the end of the setComboRootIndex slot, that sets the current index to the topmost entry.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • T Offline
              T Offline
              ThaRez
              wrote on last edited by
              #6

              Awesome! Thanks!

              1 Reply Last reply
              0

              • Login

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