Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[Solved] Changing rootpath of directory model in combo box.
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!
Edit: Please use @ tags around code sections so it becomes easier to read; Andre
goetz last edited by
This basically works for me:
class MainWindow : public QMainWindow
explicit MainWindow(QWidget *parent = 0);
void setComboRootIndex(const QModelIndex &index);
MainWindow::MainWindow(QWidget *parent) :
// 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);
// 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!
goetz last edited by
You can add
at the end of the setComboRootIndex slot, that sets the current index to the topmost entry.