[Solved] Changing rootpath of directory model in combo box.
-
wrote on 16 Sept 2011, 10:47 last edited by
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
RichardEdit: Please use @ tags around code sections so it becomes easier to read; Andre
-
wrote on 16 Sept 2011, 22:11 last edited by
This basically works for me:
@
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
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);
}
@ -
wrote on 18 Sept 2011, 19:15 last edited by
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.
-
wrote on 18 Sept 2011, 21:02 last edited by
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 -
wrote on 18 Sept 2011, 22:15 last edited by
You can add
@
ui->comboBox->setCurrentIndex(0);
@at the end of the setComboRootIndex slot, that sets the current index to the topmost entry.
-
wrote on 19 Sept 2011, 06:19 last edited by
Awesome! Thanks!
1/6