Qt World Summit: Register Today!

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

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

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

  • This basically works for me:

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

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

    Ui::MainWindow *ui;
    QFileSystemModel *_fsm;

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

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


    void MainWindow::setComboRootIndex(const QModelIndex &index)
    qDebug() << "index " << index << "=" << _fsm->filePath(index);
    // make sure the entries in the dir are loaded

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

  • 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

  • You can add


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

  • Awesome! Thanks!

Log in to reply